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']);
?>