Page 1 of 1

Script being executed twice?

Posted: Wed May 19, 2010 4:03 pm
by mattspriggs28
Hi,

I have built a basic shopping cart using Session variables, so that when a user adds an item to their basket it stores the info in a session.

When an 'add item' button is clicked, it redirects to the same page and catches an if statement if it recognises that a user has added an item to their basket.

However, in IE, if an item is added whilst the page is still loading or before a certain amount of time has passed, it adds double the amount of items to the basket, giving me the impression that the page is executing twice. However, if I wait a few seconds after the page has finished loading and then add an item, it adds the expected number of items to the basket.

This problem only crops up in IE, and works fine in all other browsers.

Anyone have any ideas? Could it be something to do with the POST request?

I've double and triple checked my code and its sound.

Thanks for your help.

This is the top of my page with the if statement that gets the file if the query string 'page' equals 'add to basket':

Code: Select all


<?php
session_start();

include_once("dbconn.php");
include_once("variables.php");
include_once("functions.php");

// Get the current page to display the content.
if ($_GET['page'] != "")
{
	if ($_GET['page'] == "addtobasket") {
		require_once("includes/add-to-basket.php");	
	}
}

Here is the content of the add-to-basket.php file:

<?php
// Flag. If an existing basket item is added again, this flag is used to state whether to 
// add a new basket item or simply update the qty of an existing basket item.
$addNewBasketItem = true;

// If the shopping basket session has not been created, create it here.
if (!isset( $_SESSION["shopping_basket_items"])) 
{ 
   	$_SESSION["shopping_basket_items"] = array(); 
}

if (!isset( $_SESSION["shopping_basket_quantities"])) 
{ 
	$_SESSION["shopping_basket_quantities"] = array();
}
if (!isset( $_SESSION["shopping_basket_item_descs"])) 
{	
	$_SESSION["shopping_basket_item_descs"] = array(); 
}
if (!isset( $_SESSION["shopping_basket_item_types"])) 
{	
	$_SESSION["shopping_basket_item_types"] = array(); 
}

// Get the item and qty that was selected.
if ($baskProdId == "") { $baskProdId = $_POST['form_prod_id']; }
if ($baskProdQty == "") { $baskProdQty = $_POST['form_quantity']; }

// Check that the item is not already in the shopping basket.
// If it is, simply add to the quantity.
$itemCount = 0;

foreach ($_SESSION["shopping_basket_items"] as $val)
{
	if ($val == $baskProdId) 
	{
		$_SESSION["shopping_basket_quantities"][$itemCount] = $_SESSION["shopping_basket_quantities"][$itemCount] + $baskProdQty;
		
                // Set flag to false, as we don't want to add a new item, but simply update the qty of an existing item.
		$addNewBasketItem = false;
	}
	$itemCount ++;
}

if ($addNewBasketItem)
{
	$_SESSION["shopping_basket_items"][] = $baskProdId;
	$_SESSION["shopping_basket_quantities"][] = $baskProdQty;
	$_SESSION["shopping_basket_item_descs"][] = "";
	$_SESSION["shopping_basket_item_types"][] = "product";
}

// get the product price details
$sqlGetProdPrice = sprintf("Select ".$_SESSION['currency_field']." from " . TBL_PREFIX . "_products where id = %s", 
	mysql_escape_string($baskProdId));
$qryGetProdPrice = mysql_query($sqlGetProdPrice) or die(mysql_error());

$rowProdPrice = mysql_fetch_assoc($qryGetProdPrice);
$tempPrice = $rowProdPrice[$_SESSION['currency_field']];

// Get the running total for the shopping basket...

$baskProdPrice = $tempPrice * $baskProdQty;
$baskProdPrice = number_format($baskProdPrice, 2, '.', '');

// If session does not exist
if ((!isset($_SESSION["shopping_basket_num_items"])) || ((!isset($_SESSION["shopping_basket_total"]))))
{
	$_SESSION["shopping_basket_num_items"] = array();
	$_SESSION["shopping_basket_total"] = array();
	
	$_SESSION["shopping_basket_num_items"] = $baskProdQty;
	$_SESSION["shopping_basket_total"] = $baskProdPrice;
} 
// If session exists
else
{
	$_SESSION["shopping_basket_num_items"] = $_SESSION["shopping_basket_num_items"] + $baskProdQty;
	$_SESSION["shopping_basket_total"] = $_SESSION["shopping_basket_total"] + $baskProdPrice;
}

header("Location: " . $_POST['form_prev_page']);
?>

Re: Script being executed twice?

Posted: Mon May 24, 2010 7:58 pm
by mecha_godzilla
I can't see anything obviously wrong in your script, but what I would suggest (for testing purposes) is that you use a session variable to count how many times a particular script is running and then echo this value back out to your page. I don't use IE much but if it doesn't like the fact that a page is being reloaded before it's loaded in its entirety then it does suggest that IE isn't handling it very gracefully - it could be some kind of caching issue so you might want to look at how your server is set up in respect of the headers it's sending (this assumes you have access to the configuration files of course.) I don't want to be a M$ basher but you know IE stinks anyway, right? Put a "Download Firefox Now!" button on there :mrgreen:

If your PHP script is being output in sections (as opposed to all the template and live data being generated beforehand and output in all pass) then you might need to find a way of making sure that the script doesn't run again if it hasn't completed. This suggestion is probably a bit vague but - as an example - at the very start of the script set a session variable to TRUE and at the very end of the script set the same variable to FALSE, then do a test at the start of the script to see if the value is TRUE, and ignore the 'adding to basket' part of the script if it is. Like I said, a bit vague :(

HTH,

Mecha Godzilla