Page 1 of 1

Inadvertant resubmit

Posted: Sun Dec 15, 2002 4:36 pm
by EricS
I'm working on a shopping cart. It uses a form to submit to say "addtocart.php". "addtocart.php" adds the item to the database and works great.

Problem: if you hit the refresh button on the browser it enters another of the same item to the cart. Also if you hit a button to continue shopping and then hit the back button on the browser, then it adds another item to the shopping cart.

Is there anyway to stop resubmitting to the cart short of disabling the refresh and back button on the browser?

Posted: Mon Dec 16, 2002 7:26 am
by Johnm
One simple way would be to set a var upon submission. Then, add a few lines that only allow that part of the script to execute when the var is set to the proper value.

Code: Select all

<?php
$track_submit_var=0;

if( isset($track_submit_var) && $track_submit_var == 0)
{
    submit the form
    set $track_submit_var to 1 
}

?>
Hope it helps.

John M

Sessions

Posted: Mon Dec 16, 2002 1:25 pm
by EricS
I figured it out, for anyone with similar problems. In my case there is only one page that sends form data to addtocart.php so I set a session variable on that page to "yes" and then check for that session variable on the addtocart page. Then after the database has been successfully updated, I set that variable to "no" and it works. No more resubmission on refresh or back button.

Thanks for the help Johnm.

Eric Stewart

Posted: Mon Dec 16, 2002 1:28 pm
by Johnm
Good, same idea, different execution.
Glad it works.

John M

Posted: Mon Dec 16, 2002 3:18 pm
by puckeye
You could also use "header("Location:thingsadded.html"); once you finish filling the database. That way if if the user reloads he'll only reload thingsadded.html which is harmless. Same thing if the user backs up since they'll back up to the form instead of the form processing page. The addtocart.php page isn't sent to the user only the thingsadded.html.

In a few words using the header command will cause the User's Browser history to look like this:
1) form to add products.
2) thanks for adding these products.

The form processing page only stays on the server.

Posted: Mon Dec 16, 2002 3:43 pm
by Johnm
Under some circumstances we delete everything in the field then insert it again in the same function to ensure that the correct values are in the db.
Example:

Code: Select all

<?php

    if(strcmp($base_number[0],"0") && $session_info['type']==$VENT )
    {
	    $sql="delete from regen_vals 
	          where job='".$job."' 
	              and base='".$base_number."' 
	              and name='JOB_NUMBER' 
	              and loc_code='".$loc_code."'";
        ifx_query($sql,$dbid);
        
	    $sql="insert into regen_vals 
	          values("".$job."","".$base_number."","S","JOB_NUMBER","".$job."",".$today.","".$loc_code."")"; 
        ifx_query($sql,$dbid);
    }
?>
This way the user can do anything they want because everytime the function is called the correct dadt ends up in the db. No worries about Forward and Back Buttons.



John M