stergosz
stergosz

Reputation: 5860

php Same form multiple times in same page

So i have the same form 2-3 times in the same page which is a poll question and its generated dynamically from the database so its the same as the others but when i click the submit button i can vote the others aswell since the button on each has the same name with the others...

how can i fix this so it wont affect the other polls?

form is like this:

<form action="" method="post" name="form">
radio buttons here...
<input type="submit" value="Vote" name="btn" />
</form>

Upvotes: 1

Views: 1974

Answers (3)

Kamil Tomš&#237;k
Kamil Tomš&#237;k

Reputation: 2437

easiest solution is to change form action - then it will point to different uri which should do the trick:

<form action=".../vote/id-of-first-poll">
...
</form>

<form action=".../vote/id-of-second-poll">
...
</form>

<form action=".../vote/id-of-third-poll">
...
</form>

then you'll just simply apply that passed id from url to your "where" in update query...

BTW: not sure how advanced your are, so if you're not using any framework, it will be probably much easier to have urls like this:

  • vote.php?id=id-of-first-poll
  • vote.php?id=id-of-second-poll
  • etc.

then you can get that id by $_GET["id"]

Upvotes: 1

rabudde
rabudde

Reputation: 7722

You have to use id field for you forms. I've also multiple forms on my sites. So I send always a hidden field with form name (id) back to server. When POST requests receives at my application I'm reading first the hidden field with form id. You could do the same, so you know the poll id. I don't understand your problem when a user doesn't select any radiobutton.

Upvotes: 1

Balanivash
Balanivash

Reputation: 6867

You can either have a different name foe each form or a hidden field in each form which tells the question id.

<form action="" method="post" name="form">
radio buttons here...
<input type="hidden" name="poll_id"  value="(Your_question_id)">
<input type="submit" value="Vote" name="btn" />
</form>

So, after this, in you SQL part just add the poll_id here in the WHERE clause. Think this should work

Upvotes: 2

Related Questions