开发者

PHP/SQL Submit button inside while loop

I got a php code that prints a table using while loop:

while ($data = mysqli_fetch_array ($result)) {

    echo '<tr>';
    echo '<td>' .$data ['commodity']. '</br>'. '</td>';
    echo '<td>' .$data ['region'].'</br>'.'</td>';
    echo '<td>' .$data ['member'].'</br>'. '</td>';
    echo '<td>' .$data ['size']. '</br>'. '</td>';
    echo '<td>' .$data ['price']. '</br>'.'</td>';
   开发者_高级运维 echo '<td>' .$data ['posted']. '</br>'.'</td>';

    echo '<TD><INPUT TYPE=BUTTON OnClick="submit_btn" NAME="accepted" VALUE="accept"></TD>';
    echo '</tr>';

            $id=$data ['id'];

This fetches the data + submit_btn. now I need the button to get the current id and store it in a variable.

big thanks for all help !


Very simple. Add a counter variable (for instance $i) with starting value of 1. before the end of the loop increase it by one ($i++). This counter will help us creating a new name for each button, because eventually having more than one button with the same name will make things go wrong. (Especially when using JS).

change the line of the button:

 echo '<TD><INPUT TYPE=BUTTON OnClick="submit_btn(this)" NAME="accepted'.$i.'" VALUE="'.$data['id'].'"></TD>';

Notice the paramater for the submit_btn function, by using it you can access "this.value" which its value is the id of the row.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜