0

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 !

2
  • 1
    so what about the $id=$data['id'] part? Commented Jul 13, 2011 at 0:04
  • Can you clarify your question? Seems ambiguous, like button needs to do something which might imply javascript, but your saying it's a PHP problem. Commented Jul 13, 2011 at 0:11

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.