1

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>
5
  • 1
    names of forms should be different Commented Jun 25, 2011 at 10:46
  • i have added a counter next to form so it goes like form1 , form2 etc.. but the problem is still there Commented Jun 25, 2011 at 10:50
  • Forms shouldn't have names at all, they should have ids Commented Jun 25, 2011 at 10:56
  • you changed the form name to ids thats fine, but are you checking which form is submitting in your server side?? ie which form is the values coming from.And do you have a WHERE clause which tells which poll to add the vote to in your SQL query?? Commented Jun 25, 2011 at 10:59
  • this is done inside a while loop where i get all the polls from my system... i dont know if this causes the problem... Commented Jun 25, 2011 at 11:12

3 Answers 3

2

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

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

1 Comment

but if a user does not select an option from the radio buttons it will show a message that the user needs to select something... so the problem is still there..
1

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.

Comments

1

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"]

1 Comment

what i was describing is an admin feature so the admin can vote when viewing the polls if not voted yet.. but i will probably remove it since as it seems its not that necessary...

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.