SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    May 2009
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How to prevent Resend for a Form after Submit?

    Hi all,
    I have recently started learning PHP and had been good till I reached forms. This is where I am stuck up -
    I have a simple contact me form that has 4 fields. After submitting the form I call a php script to update the data in a MySQL database. This part is ok. But the problem is that after update of the database, in case I click the refresh button, the data on the form gets resubmitted - that is after displaying data updated successfully, in case I refresh the page, the data just entered on the form is resubmitted and updated in the database. Any advice that I should try out?

    Thanks for the advice in advance

    Code of php script that gets called is
    Code:
    <?
    $post_vars = $HTTP_POST_VARS;
    $uname     = $post_vars['uname'];
    $email     = $post_vars['email'];
    $related   = $post_vars['related'];
    $feedback  = $post_vars['msg'];
    $flag = true;
    //Validate the Input
    //........
    if($flag)
    {
    //connect to the database
    	//Create the Query/ Insert Statement
    	$ins = "INSERT INTO feedback (";
    	$flds = "name, email, relatedto, feedback) VALUES(";
    	$vals = '"' . $uname . '","' . $email . '","' . $related . '","' . $feedback . '")';
    	$statement = $ins . $flds . $vals;
    	//Execute the statement
    	$result=mysql_query($statement);
    	if($result)
    	{
    		print("Data Updated Successfully");
    		$flag=true;
    	}
    	else
    	{
    		die("Could not insert the data in the database... <br />" . mysql_error());
    		$flag=false;
    	} 
    	mysql_close($connection);
    }
    ?>

  2. #2
    SitePoint Zealot zalucius's Avatar
    Join Date
    Jul 2007
    Location
    Denmark
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One solution could be to store the succesfull message in a session, and then use a header to reload the page, which then clears the postdata

    It could look somehting like this:
    Code PHP:
    <?php 
    session_start();
    ob_start();
    // if the data already have been processed, the session will contain a message
    if(isset($_SESSION['form_message']))
    {
    	// display the message on screen, and then clear the message.
    	print($_SESSION['form_message']);
    	unset($_SESSION['form_message']);
    }
    else
    {
    	// if no session['form_message'] exists, then test if any postdata exists, and process if needed
    	if(isset($_POST))
    	{
    		$uname     = $_POST['uname'];
    		$email     = $_POST['email'];
    		$related   = $_POST['related'];
    		$feedback  = $_POST['msg'];
    		$flag = true;
    		//Validate the Input
    		//........
    		if($flag)
    		{
    		//connect to the database
    			//Create the Query/ Insert Statement
    			$ins = "INSERT INTO feedback (";
    			$flds = "name, email, relatedto, feedback) VALUES(";
    			$vals = '"' . $uname . '","' . $email . '","' . $related . '","' . $feedback . '")';
    			$statement = $ins . $flds . $vals;
    			//Execute the statement
    			$result=mysql_query($statement);
    			if($result)
    			{
    				$_SESSION['form_message'] = "Data Updated Successfully"
    				$flag=true;
    			}
    			else
    			{
    				die("Could not insert the data in the database... <br />" . mysql_error());
    				$flag=false;
    			} 
    			mysql_close($connection);
    			if($flag)
    			{
    				header("location:" . $_SERVER['PHP_SELF']);
    				exit;
    			}
    		}
    	}
    }
    ob_end_flush();
    ?>

    It is just one way to clear the post data, im sure others have different approaches to this.

    You need some way to clear the post data, regardless of what solution you end up with, and IMHO the header is the easiest way to achieve this.
    zalucius

  3. #3
    SitePoint Member
    Join Date
    May 2009
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Tx zalucius. I had thought of using the <meta http-equiv="refresh" content="1,../index.htm" /> tag to solve the issue but I feel that using sessions is a better way of implementing the concept. Thanks again. I will give it a try it out and report back.

    Cheers,
    thePirate

  4. #4
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It might help reading the Post/Redirect/Get pattern
    my mobile portal
    ghiris.ro

  5. #5
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,276
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    Ernie ++

    this is the best way to go. While FF will say "do you want to resend data?" IE will just say, "page expired, go to hell". PRG will prevent that, and is safer, and is not just for refreshes but also stops forms with POST from breaking the Back button.

    This was my first find back when I was first looking into this problem:
    http://www.theserverside.com/tt/arti...irectAfterPost

    Oldie but goodie, and ignore the Java stuff.

    Also a good reason to do this on the server side is that of course user agents may not bother looking at those meta tags we love to add. They're not required to and many of the just ignore them.

  6. #6
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    We use that method now, as well. We experimented with doing all submits via AJAX, but of course that doesn't work with file uploads.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •