Note that there are some explanatory texts on larger screens.

plurals
  1. POShopping cart functionality, updating and clearing fields
    primarykey
    data
    text
    <p>I have created a very basic ecommerce application and this is the entire code for the shopping cart. I am having trouble getting the functions to update the cart,(for example, I add 1 product into the shopping cart, then go into the cart and wish to change the amount ordered to 10, the code will not allow me at present) and to clear fields, I have the code but it just does nothing. Thanks</p> <pre><code> &lt;? include("includes/db.php"); include("includes/functions.php"); if($_REQUEST['command']=='delete' &amp;&amp; $_REQUEST['pid']&gt;0){ remove_product($_REQUEST['pid']); } else if($_REQUEST['command']=='clear'){ unset($_SESSION['cart']); } else if($_REQUEST['command']=='update'){ $max=count($_SESSION['cart']); for($i=0;$i&lt;$max;$i++){ $pid=$_SESSION['cart'][$i]['productid']; $q=intval($_REQUEST['product'.$pid]); if($q&gt;0 &amp;&amp; $q&lt;=999){ $_SESSION['cart'][$i]['qty']=$q; } else{ $msg='Some proudcts not updated!, quantity must be a number between 1 and 999'; } } } ?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt; &lt;title&gt;Shopping Cart&lt;/title&gt; &lt;script language="javascript"&gt; function del(pid){ if(confirm('Do you really mean to delete this item?')){ document.form1.pid.value=pid; document.form1.command.value='delete'; document.form1.submit(); } } function clear_cart(){ if(confirm('This will empty your shopping cart, continue?')){ document.form1.command.value='clear'; document.form1.submit(); } } function update_cart(){ document.form1.command.value='update'; document.form1.submit(); } &lt;div id="login"&gt; &lt;?php session_start(); // dBase file include "dbConfig.php"; if ($_GET["op"] == "login") { if (!$_POST["username"] || !$_POST["password"]) { die("You need to provide a username and password."); } // Create query $q = "SELECT * FROM `dbUsers` " ."WHERE `username`='".$_POST["username"]."' " ."AND `password`=PASSWORD('".$_POST["password"]."') " ."LIMIT 1"; // Run mysql query $r = mysql_query($q); if ( $obj = @mysql_fetch_object($r) ) { // Login good, create session variables $_SESSION["valid_id"] = $obj-&gt;id; $_SESSION["valid_user"] = $_POST["username"]; $_SESSION["valid_time"] = time(); // Redirect to member page Header("Location: index.php?p=members"); } else { // Login not successful die("Sorry, could not log you in. Wrong login information."); } } else { //If all went right the Web form appears and users can log in echo "&lt;form action=\"?p=login&amp;op=login\" method=\"POST\"&gt;"; echo "Username: &lt;input name=\"username\" size=\"20\"&gt;&lt;br /&gt;"; echo "Password: &lt;input type=\"password\" name=\"password\" size=\"20\"&gt;&lt;br /&gt;"; echo "&lt;input type=\"submit\" value=\"Login\"&gt;"; echo "&lt;/form&gt;"; } ?&gt; &lt;li&gt;&lt;a href="index.php?p=register"&gt;Register&lt;/a&gt;&lt;/li&gt;&lt;/div&gt; &lt;/ul&gt; &lt;/div&gt; &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form name="form1" method="post"&gt; &lt;input type="hidden" name="pid" /&gt; &lt;input type="hidden" name="command" /&gt; &lt;div style="margin:0px auto; width:550px;" &gt; &lt;div style="padding-bottom:10px"&gt; &lt;div style="padding-top:50px"&gt; &lt;h1 align="center"&gt;Shopping Basket&lt;/h1&gt; &lt;input type="button" value="Continue Shopping" onclick="window.location='index.php?p=products'" /&gt; &lt;/div&gt; &lt;div style="color:#F00"&gt;&lt;?=$msg?&gt;&lt;/div&gt; &lt;table border="0" cellpadding="5px" cellspacing="1px" style="font-family:Verdana, Geneva, sans-serif; font-size:12px; background-color:#E1E1E1" width="100%"&gt; &lt;? if(is_array($_SESSION['cart'])){ echo '&lt;tr bgcolor="#FFFFFF" style="font-weight:bold"&gt;&lt;td&gt;Serial&lt;/td&gt;&lt;td&gt;Name&lt;/td&gt;&lt;td&gt;Price&lt;/td&gt;&lt;td&gt;Qty&lt;/td&gt;&lt;td&gt;Amount&lt;/td&gt;&lt;td&gt;Options&lt;/td&gt;&lt;/tr&gt;'; $max=count($_SESSION['cart']); for($i=0;$i&lt;$max;$i++){ $pid=$_SESSION['cart'][$i]['productid']; $q=$_SESSION['cart'][$i]['qty']; $pname=get_product_name($pid); if($q==0) continue; ?&gt; &lt;tr bgcolor="#FFFFFF"&gt;&lt;td&gt;&lt;?=$i+1?&gt;&lt;/td&gt;&lt;td&gt;&lt;?=$pname?&gt;&lt;/td&gt; &lt;td&gt;&amp;pound; &lt;?=get_price($pid)?&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="product&lt;?=$pid?&gt;" value="&lt;?=$q?&gt;" maxlength="3" size="2" /&gt;&lt;/td&gt; &lt;td&gt;&amp;pound; &lt;?=get_price($pid)*$q?&gt;&lt;/td&gt; &lt;td&gt;&lt;a href="javascript:del(&lt;?=$pid?&gt;)"&gt;Remove&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt; &lt;? } ?&gt; &lt;tr&gt;&lt;td&gt;&lt;b&gt;Order Total + Shipping: &amp;pound&lt;?=get_order_total() +5.00?&gt;&lt;/b&gt;&lt;/td&gt;&lt;td colspan="5" align="right"&gt;&lt;input type="button" value="Clear Cart" onclick="clear_cart()"&gt;&lt;input type="button" value="Update Cart" onclick="update_cart()"&gt;&lt;input type="button" value="Place Order" onclick="window.location='index.php?p=billing'"&gt;&lt;/td&gt;&lt;/tr&gt; &lt;? } else{ echo "&lt;tr bgColor='#FFFFFF'&gt;&lt;td&gt;There are no items in your shopping cart!&lt;/td&gt;"; } ?&gt; &lt;/table&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload