Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/202299-script-being-executed-twice/
Share on other sites

Different browsers request pages twice for different reasons. IE generally will request an index.xxx page twice when it tries to read the favicon file.

 

However, you are redirecting to the same page and it is likely that you don't have an exit; statement after your header() redirect to prevent the remainder of the code on the page from being executed and that code is probably clearing or setting a value that is preventing your logic from detecting if the item has already been added.

 

It would take seeing your code to be able to determine if it is or is not responsible for the symptom.

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

 

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

 

Can't seem to get my head around it. To cut down the code above, I've illustrated below:

 

Product Page -> User adds item -> redirects to same page -> includes 'add-to-basket' file to process item -> continues executing rest of page.

 

Where in here would it redirect for a second time?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.