0

Okay, I'm sure I'm missing something simple, but I'm populating a hidden field on a form with data from a MySQL While statement. the data shows up properly when on the page, but when submitted, the data submitted is always from the last loaded id.

while ($row = mysql_fetch_assoc($result)) {
    $id = $row['id'];
    echo '<form name="voting" action="thanks_for_voting.php" method="POST">
    <align="right"><input type="hidden" name="id" value="'.$id.'"
    <br><input type="submit" value="VOTE FOR '.$id.'"><br>';
}

What am I doing wrong? I'm trying to keep a single call to my DB to prevent overloading, I assume there must be a simple way to achieve my goal? But what?

Machineadict solved my problem below by noticing I hadn't been closing my form. I closed it, and the problem was solved. Thanks!

3 Answers 3

2

The form tag is not closed. Lose the html tags, you don't need them. And it's alright to use forms in loops, you don't have to change the names of inputs.

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

1 Comment

You got it! I forgot to close my form so it was left open!
1

As machineaddict has mentioned, if you want to use multiple forms you must close the tag of the preceding one before you open the next.

You can also do this with a single form with a single hidden field assigning it a specific value depending on which button is clicked:

echo '<html><form name="voting" action="thanks_for_voting.php" method="POST"><align="right">';
echo '<input type="hidden" name="id" value="-1">';
while ($row = mysql_fetch_assoc($result)) {
  $id = $row['id'];
  echo '<input type="button" value="VOTE FOR '.$id.'" onclick="javascript:document.forms[\'voting\'].elements[\'id\'].value='.$id.'; document.forms[\'voting\'].submit();">';
}
echo '</form></html>';

Here you have a button for each of the IDs from the database. Each button executes the following JavaScript code in its onclick handler:

document.forms['voting'].elements['id'].value = YOUR_ID;
document.forms['voting'].submit();

This sets the ID to the desired value and then submits the form. You may want to put this code aside in a function and put the name of the function into the onclick handler.

Also, you probably don't want to generate multiple <html> tags.

1 Comment

What do you think the easiest way would be to accomplish that in the loop? I'm outputting 3 results with every loop by design.
0

Your <form> is inside the while loop. And as Adam Zalcman says, use a unique name.

Try

echo '<html><form name="myform" action="thanks_for_voting.php" method="POST"><align="right">';
while ($row = mysql_fetch_assoc($result)) {
  $id = $row['id'];
  echo '<input type="hidden" name="id[]" value="'.$id.'"><br><input type="submit" value="VOTE FOR '.$id.'">';
}
echo '<br></html>';}

This will post an array of values, which you can retrieve as $_POST['id'][0], $_POST['id'][1] etc

2 Comments

actually. This is wrong. I've missed the point of what you're trying to do.
my goal is to have three submit button on the page, each one for a different item, containing the unique id. that way when a user clicks the "vote for ___" button, it submits to the next page the id data from that item.

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.