Jump to content

Validate Form Re-Submitting On Refresh


Go to solution Solved by Christian F.,

Recommended Posts

Hello.

 

I'm having a difficulty with my code and I'd appreciate some assistance.

 

The below is a poll which is supposed to take user input in the form of radio-boxes and display the amount of users who have selected the given option. On the first run, it works fine, but whenever the page is refreshed, the form resubmits and the vote is entered again. I know this can be easily solved by posting the form information to another page and then redirecting back - and that's how I usually handled the problem in the past - but I'm wandering if there's a better way. Anybody got any ideas? Thanks in advance.

 

<!--start of poll-->
		<div id = "fullPoll"><!--POLL FORM-->
			<?php
			printRibbon('pollTag', 'Poll:' , 55, -30, 0, 20);
				include 'sitePoll.php';
				
					if(isset($_POST['option'])){//RADIOBOX
						if(!empty($_POST['option'])){
							$chosenOption = $_POST['option'];
						
							$queryVotes = mysql_query("SELECT ".$chosenOption." FROM sitepoll");
							$currentVotes = mysql_result($queryVotes, 0);
							$updateVotes = $currentVotes + 1;
						
							mysql_query("UPDATE sitepoll SET ".$chosenOption." = '".$updateVotes."'");
							header('Location: /No Common Theme/activeSourceTabLink.php');
						}else{
							?>
						<div class = "errorsDiv">
							Choose an option, Scrub.
						</div>
						<?php
							
						}
					}
					
					
			?>
		</div>
		<!--end of poll-->

 

 

Link to comment
https://forums.phpfreaks.com/topic/274997-validate-form-re-submitting-on-refresh/
Share on other sites

You can set a session variable that says that the user has already answered the poll. Or you could use form tokens. (Send a form token with the form.) I think you should use both, as form tokens help with security and cross-site scripting as well. You might as well learn it now.

  • Solution

Best way is to send a Location HTTP header after saving the results to the database. That way if the user refreshes the page he'll send a GET request, instead of the previous POST request.

This can be done with the header function in PHP.

Edited by Christian F.

Best way is to send a Location HTTP header after saving the results to the database. That way if the user refreshes the page he'll send a GET request, instead of the previous POST request.

This can be done with the header function in PHP.

 

I've done something similar to what you suggested, and it worked out great :)

Thanks.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.