在下拉列表更改事件中从购物车中删除项目

时间:2014-03-10 10:50:21

标签: php jquery ajax html5

我正在建立一个电子商务系统,用户可以从类别下拉列表中选择一个项目。 我希望用户只从每个类别中添加一个项目,当他们从该类别更改项目时,必须删除购物车中已存在的项目。每个类别项目都具有相同的项目ID,以便用户无法添加同一类别中的两个或更多项目。

我可以将商品添加到购物车,但我很难确保如果购物车中已存在同一类别的商品,则必须先将其删除。 例如如果用户想要将Sony Chase添加到购物车并且LG Chase已经在购物车中,则LG追逐必须由Sony替换,因为它们属于同一类别 Chassis < / strong>即可。我不知道我是否清楚

<form method="post" action="" class="jcart">
<fieldset>
<input type="hidden" name="my-item-url" value="" />
    <input type="hidden" name="my-item-qty" value="1"/>
    <table>
    <tr><td width="180">
<select name="chasis" id="chasis" onChange="grabInfo(this.value)" class="styled-select">
    <option value="">-- Select Chasis --</option>
    <?php 
            $queryChasis = mysql_query("SELECT * FROM chasis");     

            while ($row = mysql_fetch_array($queryChasis))
            {
            ?>
                <option value="<?php echo $row['ItemID']; ?>"><?php echo $row['CName']; ?></option>
            <?php
            } 
            ?>
      </select>
      </td>
      </tr>
      </table>
      </fieldset>
 </form>


<!-- Ajax Passing the ID -->
function grabInfo(str)
{
if (str=="")
{
    document.getElementById("contentDiv").innerHTML = "";
    return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        document.getElementById("contentDiv").innerHTML = "&nbsp;";
        document.getElementById("contentDiv").innerHTML = xmlhttp.responseText;
    }
}
xmlhttp.open("GET","getinfoChasis.php?q="+str,true);
xmlhttp.send();
}

如果用户尝试从产品页面向购物车添加内容。 这是我尝试从同一类别中删除现有项目的地方

if (isset($_GET["q"])) {
$pid = $_GET["q"];
$wasFound = false;
$i = 0;
// If the cart session variable is not set or cart array is empty
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { 
    // RUN IF THE CART IS EMPTY OR NOT SET
    $_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1));
} else {
    // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
    foreach ($_SESSION["cart_array"] as $each_item) { 
          $i++;
          while (list($key, $value) = each($each_item)) {
              if ($key == "item_id" && $value == $pid) {

                  // That item is in cart already so let's remove
                  $key_to_remove = $pid;
                if (count($_SESSION["cart_array"]) <= 1) {
                    unset($_SESSION["cart_array"]);
                } else {
                    unset($_SESSION["cart_array"]["$pid"]);
                    sort($_SESSION["cart_array"]);
                }
                  $wasFound = true;
              } // close if condition
          } // close while loop
       } // close foreach loop
       if ($wasFound == false) {
           array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1));
       }
}
}

渲染购物车以供用户在页面上查看

$cartOutput = "";
$cartTotal = "";
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
$cartOutput .= "<h2 align='center'>Your shopping cart is empty</h2>";
}
else
{
$i = 0; 
foreach ($_SESSION["cart_array"] as $each_item) {
    $i++;
    $item_id = $each_item['item_id'];
    $sql = mysql_query("SELECT * FROM chasis WHERE ID='$item_id' LIMIT 1");
    while ($row = mysql_fetch_array($sql)) {
        $product_name = $row["CName"];
        $price = $row["Price"];
    }
    $cartTotal = $price + $cartTotal;
    /*$cartOutput .= "<h2>Cart Item $i</h2>";
    $cartOutput .= "Item id: ".$each_item['item_id']."<br/>";
    $cartOutput .= "Quantity: ".$each_item['quantity']."<br/>";*/
    $cartOutput .= "<tr><td>".$product_name."</td>";
    $cartOutput .= "<td>".$price."</td></tr>";
}
//total price
$cartTotal = $cartTotal;
}
echo '<table border="1" cellspacing="0" cellpadding="6">
<tr bgcolor="#ccffcc"><td width="220">Item Name</td>
<td width="120">Price</td></tr>';
echo $cartOutput;
echo '</table>';

echo '<table border="1" cellspacing="0" cellpadding="6">
<tr align="right"><td width="220"><strong>Total Price:</strong></td>
<td width="120">'.$cartTotal.'</td></tr></table>';
?>

1 个答案:

答案 0 :(得分:1)

我已经开发了3个电子商务应用程序,我以标准方式解决了这些问题。

  1. 首先,您需要保存Category ID以及购物车中添加的任何产品的Product ID。现在让我们来解决你的问题。您想为每个类别仅为任何用户添加一个项目吗?假设我在JUICE类别下有几款产品。例如,如果JUICE的Category ID为4,则JUICE类别下的所有产品的Category ID均为4。
  2. 您可以做的是,当用户从下拉列表中选择产品时,您可以使用ajax页面中的ajax传递Product IDCategory ID。请尝试使用会话数组作为使用会话,因为数组不是专业方法,如果你的数组太长,它会影响加载速度。因此,您可以执行Product IDCategory ID后,可以使用SELECT查询检查购物车,以查找表中是否已存在相同的类别产品。如果是,则运行DELETE查询以删除产品,然后运行INSERT查询以插入新产品。否则,您可以直接使用INSERT查询添加新产品。

    如果您需要了解更多详细信息,例如您需要处理的问题,请告知我们。我会尽力帮助你。

    问候。