Inadvertant resubmit

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
EricS
Forum Contributor
Posts: 183
Joined: Thu Jul 11, 2002 12:02 am
Location: Atlanta, Ga

Inadvertant resubmit

Post 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?
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post 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
EricS
Forum Contributor
Posts: 183
Joined: Thu Jul 11, 2002 12:02 am
Location: Atlanta, Ga

Sessions

Post 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
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post by Johnm »

Good, same idea, different execution.
Glad it works.

John M
User avatar
puckeye
Forum Contributor
Posts: 105
Joined: Fri Dec 06, 2002 7:26 pm
Location: Joliette, QC, CA
Contact:

Post 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.
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post 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
Post Reply