Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

When click continue shopping it adds a item to Cart?? 1

Status
Not open for further replies.

tshey

Technical User
Dec 28, 2005
78
AU
Hi Everyone,
I have a cart operational, except when an item is added to the cart, its qty changed, it saved (to this point it all works fine) however when I click on continue shopping button it adds another item qty to the new item. Then I click on continue shopping for a second time and it takes me to where i want to go. What could be the issue? Here is the code for my cart, what else would help solve this prob?
Code:
<?php

  // The shopping cart needs sessions, so start one
  session_start();
  include ('book_sc_fns.php');
  $new;
if (isset($_GET['new'])) {
  $new = ($_GET['new']);
}

  if($new)
  {
    //new item selected
    if(!isset($HTTP_SESSION_VARS['cart']))
    {
      $HTTP_SESSION_VARS['cart'] = array();
      $HTTP_SESSION_VARS['items'] = 0;
      $HTTP_SESSION_VARS['total_price'] ='0.00';
    }
    if(isset($HTTP_SESSION_VARS['cart'][$new]))
      $HTTP_SESSION_VARS['cart'][$new]++;
    else 
      $HTTP_SESSION_VARS['cart'][$new] = 1;
    $HTTP_SESSION_VARS['total_price'] =       
                                      calculate_price($HTTP_SESSION_VARS['cart']);
    $HTTP_SESSION_VARS['items'] = calculate_items($HTTP_SESSION_VARS['cart']);

  }
  if(isset($HTTP_POST_VARS['save']))
  {   
    foreach ($HTTP_SESSION_VARS['cart'] as $design_id => $qty)
    {
      if($HTTP_POST_VARS[$design_id]=='0')
        unset($HTTP_SESSION_VARS['cart'][$design_id]);
      else 
        $HTTP_SESSION_VARS['cart'][$design_id] = $HTTP_POST_VARS[$design_id];
    }
    $HTTP_SESSION_VARS['total_price'] = 
calculate_price($HTTP_SESSION_VARS['cart']);
    $HTTP_SESSION_VARS['items'] = calculate_items($HTTP_SESSION_VARS['cart']);
  }

  do_html_header('Your shopping cart');
do_cart();
do_search();
 // get categories out of database
  $cat_array = get_categories();
 // display as links to cat pages
  display_categories($cat_array); 
?></div></div>
 
<div id="content" style="position:absolute; width:562px; height:450px; z-index:4; left: 173px; top: 10px; overflow: scroll;">
   <div align="center"><img src="images/shoppingcart.gif" width="129" height="40"></div><br>
    
A 99 cent service fee is included for purchases under $10. Once you have selected all your embroidery designs and collections, make your payment with paypal by clicking on the "go to checkout" button.
<div align="center"><img src="images/tenpercent.gif" width="206" height="66"></div>
<?php
  if($HTTP_SESSION_VARS['cart']&&array_count_values($HTTP_SESSION_VARS['cart'])){
    display_cart($HTTP_SESSION_VARS['cart']);
 ?>
<hr><br>
<div align="center"><a href="javascript:history.go(-1)"><img src="images/continueshopping.gif" alt="Continue shopping" width="110" height="43" border="0"></a></div>
<hr>
<br><br><form action="[URL unfurl="true"]https://www.paypal.com/cgi-bin/webscr"[/URL] method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="business" value="info@cds.com">
<input type="hidden" name="upload" value="1"> 
<input type="hidden" name="currency_code" value="AUD"> 
  <?php paypal_output($HTTP_SESSION_VARS['cart']); ?>
  <div align="center"><strong>Ready to Buy!</strong><br><br>Make payments with PayPal - it's fast, free and secure!<br>

<input type="image" src="[URL unfurl="true"]http://www.carolinesdigistitching.com/images/gotocheckout.gif"[/URL] border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
  </div>
</form>
<?php
} 
else{
echo "Your shopping cart is empty!";
}
?>

</div><?php
do_html_footer();
?>
 
i suspect it does not simply increment the quantity but in fact repeat whatever the last server action was. this is because you are using javascript:history.go(-1) to control user destinations.

i would rather see the continue shopping button being a server interaction that takes the user to the last place he was looking at (or a default shopping page) by way of querying a session variable.

i.e. if a user was looking at product Y your code would store this in a session and then, on pressing "continue shopping" the session variable would act as a pointer to your code to display the relevant page.

separately you should alter your code to protect against inadvertent refreshes/back buttons having an effect on your session variables. I do this by storing the same uniqueid in a hidden field on the form and in a session variable. if the two values match i allow the form interaction and then delete the session variable. If there is no match, the form interaction is ignored and if there is a match but the form is not validated i interrupt the interaction and redisplay the form to the user without deleting the session variable.

lastly the use of long names for superglobals is deprecated and will probably break your code in the not too distant future. change all instances of $HTTP_SESSION_VARS for $_SESSION and $HTTP_POST_VARS for $_POST etc.

 
what do mean when you say "the use of long names for superglobals is deprecated and will probably break your code in the not too distant future" What do you mean by break my code?
 
long variables have been deprecated since php 4.1.0. they have been replaced with the short form $_SERVER (etc). these shorter forms have a further advantage that they are superglobals (automatically available in the global scope).

php has supported these long variable names in later versions but there is no guarantee that it will continue to do so.

if php stop supporting long variables (or if the register_long_arrays directive is turned off in php.ini) then your code will no longer work. that's what i mean by "break"!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top