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?
Inadvertant resubmit
Moderator: General Moderators
- Johnm
- Forum Contributor
- Posts: 344
- Joined: Mon May 13, 2002 12:05 pm
- Location: Michigan, USA
- Contact:
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.
Hope it helps.
John M
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
}
?>John M
Sessions
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
Thanks for the help Johnm.
Eric Stewart
- puckeye
- Forum Contributor
- Posts: 105
- Joined: Fri Dec 06, 2002 7:26 pm
- Location: Joliette, QC, CA
- Contact:
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.
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.
- Johnm
- Forum Contributor
- Posts: 344
- Joined: Mon May 13, 2002 12:05 pm
- Location: Michigan, USA
- Contact:
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:
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
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);
}
?>John M