tags:

views:

212

answers:

5

I'm trying to make a page in php that takes rows from a database, displays them, and then give the viewer a chance to upvote or downvote a specific entry. Here is a snippet:

echo("<form action=\"vote.php\" method=\"post\"> \n");
echo("<INPUT type=\"hidden\" name=\"idnum\" value=\"".$row[0]."\">");
echo("<INPUT type=\"submit\" name=\"up\" value=\"Upvote.\">  \n");
echo("<INPUT type=\"submit\" name=\"down\" value=\"Downvote\"> ");
echo("<form/>\n");

The problem is when I hit a submit button, the value for idnum that gets sent is based on the one farthest down it seems. So my questions is, when a submit button is pressed, are the values for all inputs on a page sent?

+3  A: 

Your form is not closed properly. Use </form> instead of <form/>.

Greg Hewgill
+3  A: 

The problem with your html is that you should have </form>, not <form/>.

Kibbee
+8  A: 

Your form tag isn't closed properly. You have <form/>, but it should be </form>.

This makes the entire page a form so it sends all the inputs. With a form that is closed properly though, it will only send the inputs within the form tags that the pressed button was in.

yjerem
A: 

Wow, I feel like a dolt. Thanks for the quick answers. You've all been upvoted accordingly.

Eugene M
Thanks for posting an actual code snippet that allowed easy diagnosis of the problem. :)
Greg Hewgill
+4  A: 

While this wouldn't have helped with this particular issue, I would recommend not mixing your markup with your logic wherever possible. This is quite a lot more readable, and quite a lot more editable as well:

<form action="vote.php" method="post">
    <input type="hidden" name="idnum" value="<?php echo $row[0]; ?>">
    <input type="submit" name="up" value="Upvote.">
    <input type="submit" name="down" value="Downvote">
</form>
eyelidlessness