jamiet757 Posted February 26, 2010 Share Posted February 26, 2010 I have a cart on my website that uses sessions (I think) to figure out what is in the cart. The cart contents appear on the sidebar, and when you click view cart, it appears on the sidebar and the main window. I like this functionality, however the way it was set up was to hide the sidebar unit when the page was the shopping cart, and then remove the item with a fancy-shmancy javascript function. I don't like the way the javascript behaves, and I just want it to remove it and reload the page. There is a shopping_cart_delete.php file, but I can't really figure out how to make it remove the item from the cart. Here is some of the code of the various files: shopping_cart_delete.php: the file that will be linked to in order to delete the item from the cart. <?include("../admin/function/db.php");?> <?include("JsHttpRequest.php");?> <? $JsHttpRequest =& new JsHttpRequest($mtg); $id=(int)$_REQUEST["id"]; if(!isset($_SESSION["product_id"])){session_register('product_id');$_SESSION["product_id"]="0";} if(!isset($_SESSION["product_qty"])){session_register('product_qty');$_SESSION["product_qty"]="0";} $pp=explode("|",$_SESSION["product_id"]); $qq=explode("|",$_SESSION["product_qty"]); $p2=""; $q2=""; for($i=0;$i<count($pp);$i++) { if($id!=(int)$pp[$i]) { if($i!=0){$p2.="|";$q2.="|";} $p2.=$pp[$i]; $q2.=$qq[$i]; } } $_SESSION["product_id"]=$p2; $_SESSION["product_qty"]=$q2; if($_SESSION["product_id"]!="0") { include("shopping_cart_content.php"); } else { echo(Your shopping cart is empty!); } ?> shopping_cart_content.php: the file that displays the content of the shopping cart, along with the remove from cart link <? if(!isset($_SESSION["product_id"])){session_register('product_id');$_SESSION["product_id"]="0";} if(!isset($_SESSION["product_qty"])){session_register('product_qty');$_SESSION["product_qty"]="0";} $pp=explode("|",$_SESSION["product_id"]); $qq=explode("|",$_SESSION["product_qty"]); ?> <br /> <table border="0" cellpadding="5" cellspacing="1" class="tborder"> <tr> <td class="theader"><b>ID #</b></td> <td class="theader"><b>Dimensions</b></td> <td class="theader"><b>Price</b></td> <td class="theader">Remove</td> </tr> <? $total=0; for($i=1;$i<count($pp);$i++) { //Download items $sql="select id,name,price,id_parent,url,shipped from items where id=".(int)$pp[$i]; $dr->open($sql); if(!$dr->eof) { $folder=""; $url=item_url($dr->row["id_parent"]); $sql="select id_parent,folder,title from photos where id_parent=".(int)$dr->row["id_parent"]; $rs->open($sql); if(!$rs->eof) { $folder=$rs->row["folder"]; $size = getimagesize($s3->getAuthenticatedURL("jamesti1","clearerimages/content/".$folder."/".$dr->row["url"], 30)); $rw=$size[0]; $rh=$size[1]; $sql="select * from sizes where title='".$dr->row["name"]."'"; $ds->open($sql); if(!$ds->eof) { if($ds->row["size"]!=0) { if($rw>$rh) { $rw=$ds->row["size"]; if($rw!=0) { $rh=round($size[1]*$rw/$size[0]); } } else { $rh=$ds->row["size"]; if($rh!=0) { $rw=round($size[0]*$rh/$size[1]); } } } } } ?> <tr style="vertical-align:top"> <td class="tcontent"><a href="<?=$url?>"><?=$dr->row["id_parent"]?></a></td> <td class="tcontent"><?if(isset($size[1])){?><?=$rw?>x<?=$rh?><?}?></td> <td class="tcontent"><span class="price">1 Credit</span></td> <td class="tcontent"><a href="#" onClick="cart_delete(<?=(int)$pp[$i]?>);">Remove</a></td> </tr> <? $total+=$dr->row["price"]*$qq[$i]; } } ?> <tr> <td colspan="5" class="tcontent" align="right"><b>Total:</b> <span class="price"><?=currency(1);?><?=float_opt($total,2,true)?> <?=currency(2);?></span></td> </tr> </table><input class='isubmit' type="button" class="ft" value="Checkout »" onClick="location.href='<?=site_root?>/members/checkout.php'" style="margin-top:5px"> shopping_cart.php: the main file that handles the sessions and javascript function: <?$site="shopping_cart";?> <?include("../admin/function/db.php");?> <?include("../inc/header.php");?> <h1>Shopping Cart</h1> <?include("checkout_menu.php");?> <? if(!isset($_SESSION["product_id"])){session_register('product_id');$_SESSION["product_id"]="0";} if(!isset($_SESSION["product_qty"])){session_register('product_qty');$_SESSION["product_qty"]="0";} if($_SESSION["product_id"]!="0") { $pp=explode("|",$_SESSION["product_id"]); $qq=explode("|",$_SESSION["product_qty"]); ?> <script type="text/javascript" language="JavaScript" src="<?=site_root?>/members/JsHttpRequest.js"></script> <script type="text/javascript" language="JavaScript"> function cart_delete(value) { var req = new JsHttpRequest(); // Code automatically called on load finishing. req.onreadystatechange = function() { if (req.readyState == 4) { document.getElementById('shopping_cart').innerHTML =req.responseText; } } req.open(null, '<?=site_root?>/members/shopping_cart_delete.php', true); req.send( {id: value} ); } function cart_change(value,value2) { var req = new JsHttpRequest(); // Code automatically called on load finishing. req.onreadystatechange = function() { if (req.readyState == 4) { document.getElementById('shopping_cart').innerHTML =req.responseText; } } req.open(null, '<?=site_root?>/members/shopping_cart_change.php', true); req.send( {id: value,qty: value2} ); } </script> <div id="shopping_cart" name="shopping_cart"><?include("shopping_cart_content.php");?></div> <?}else{?> <div id="shopping_cart" name="shopping_cart">Your shopping cart is empty!</div> <?}?> <?include("../inc/footer.php");?> Can someone help me figure out how to make shopping_cart_delete.php just remove the file from the cart, and then return to the cart page? Then on the cart page, I can just make the Remove link point to the _delete.php file, instead of using the javascript Onclick function. Link to comment https://forums.phpfreaks.com/topic/193480-help-with-session-cart/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.