Unstoppable Domains

Form resubmitting on page reload?

Spaceship Spaceship
Watch

whyme953

Established Member
Impact
1
if i submit the form and than hit reload it adds that amount to the cart again.
e.g. if i add 3 of an item to the cart, then hit reload it will add another 3, if i hit reload again, another 3 etc.

any advice for how to fix this?

thanks a lot.

PHP:
		<?php
			if (isset ($_POST['submitted'])){
				$add= (int) $_POST['quantity'];
				if ($add > 0){//good to go
					if (isset($_SESSION[cart][$pid])){//item in cart, adding more.
					$_SESSION[cart][$pid]['quantity']+=$add;
					}else{//item not in cart yet
						$_SESSION[cart][$pid] = array ('quantity' => "$add", 'price' => "$prize[price]");
					}
					echo "$add tickets for this prize added to your cart";
				}//end of if $add >0
				else{
				echo ('Please enter the number of tickets you would like to purchase');
				}
			}//end of if submitted
		?>
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Well, you're re-submitting the POST variable "submitted" each time you reload. Your browser should give you a message saying you are doing this.

I'd recommend having a file that does the processing of adding an item to the cart, then using the header("Location: page.php"); function to redirect to the page you want to display AFTER adding the item(s). This could probably be the cart view. When you refresh the cart, then, it won't re-add the items.
 
0
•••
Dynadot — .com Registration $8.99Dynadot — .com Registration $8.99
Appraise.net

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Payment Flexibility
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back