添加项目并停留在当前页面

问题描述

| 我有以下代码可以添加到购物车按钮:
if (isset($_POST[\'pid\'])) {
$pid = $_POST[\'pid\'];
$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 adjust its quantity using array_splice()
                  array_splice($_SESSION[\"cart_array\"],$i-1,1,array(array(\"item_id\" => $pid,\"quantity\" => $each_item[\'quantity\'] + 1)));
                  $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));
       }
}
exit();
}
和我的添加按钮:
<form id=\"bd_itm1\" name=\"bd_itm1\" method=\"post\" action=\"help_scripts/cart_functions.php\">
      <input type=\"hidden\" name=\"pid\" id=\"pid\" value=\"\'. $id . \'\" />
      <input type=\"submit\" name=\"button\" id=\"button\" value=\"Add to Cart\" />
</form>
当我单击它时,它将带我到cart.php,因为它告诉我它与标题(位置)有关。 但我不希望如此。我想与广告按钮保持在同一页面上。 现在,如果我删除标题(位置),它将带我到空白页。 (但仍将其添加到购物车中) 我的日志中没有错误。当然。 :) 许多人都在谈论有关ajax和jquery的事情...但是我对它们并不熟悉! 谢谢 我的dynamicList代码:(我不知道发生了什么,但是现在它向我显示了您的问题)
$sql = mysql_query(\"SELECT * FROM products WHERE category=\'body\' ORDER BY id ASC\");
$productCount = mysql_num_rows($sql);
// count the output amount
if ($productCount > 0) {
        $i=0;
        $dynamicListBody = \'<table width: 90%; margin-right: auto; margin-left: auto; color: #00E6AA;>\';
        while($row = mysql_fetch_array($sql)){
            $id = $row[\"id\"];
            $product_name = $row[\"product_name\"];
            $details = $row[\"details\"];
            $price = $row[\"price\"];
            $date_added = strftime(\"%b %d,%Y\",strtotime($row[\"date_added\"]));
        $dynamicListBody .= ($i==0) ? \'<tr>\':\'\';
        $dynamicListBody .= \'<td width=\"10%\">
                    <img style=\"border:#666 1px solid;\" src=\"../stock_photos/\' . $id . \'.png\" height=\"80px\" width=\"40px\" alt=\"\' . $product_name . \'\" />
                     </td>
                     <td width=\"35%\">
                    <span class=itmttl>\' . $product_name . \'</span>
                    <br />
                    <span class=text>\' . $details . \'
                    <br />
                    €\' . $price . \'</span>
                    <br />
                      <form name=\"bd_itm\" id=\"bd_itm\" method=\"post\" action=\"help_scripts/cart_functions.php\">
                    <input type=\"hidden\" name=\"pid\" id=\"pid\" value=\"\' . $id . \'\" />
                    <input type=\"submit\" name=\"button\' . $id . \'\" id=\"button\' . $id . \'\" value=\"Add to Cart\" />
                    <br />
                    <br />
                      </form>
                     </td>\';
        $dynamicListBody .= ($i==1) ? \'</tr>\':\'\';
        $i++;
        ($i==2) ? $i=0:\'\';
        }
        $dynamicListBody.=\'</table>\';
        } else {
            $dynamicListBody = \"We have no products listed in our store yet\";
            }
        mysql_close();
?>
    

解决方法

如果您不介意从按钮执行POST后刷新页面,则可以在php代码中添加重定向以将您带回到按钮处于打开状态的页面。
header(\'Location: http://domain.com/button/page.php\');
exit();
如果您不希望刷新页面,则必须使用AJAX。 使用jQuery:
$(\':submit\').click(function() {
    var keyValues = {
        pid : $(this).parent().find(\'input[name=\"pid\"]\').val()
    };
    $.post(\'help_scripts/cart_functions.php\',keyValues,function(rsp) {
        // make your php script return some xml or json that gives the result
        // rsp will be the response
    });
    return false; // so the page doesn\'t POST
});
将上述javascript放到随按钮页面一起加载的.js文件中。 您可能需要在按下按钮时运行的php中进行此更改:
$_SESSION[\"cart_array\"] = array(0 => array(\"item_id\" => $pid,\"quantity\" => 1));
将以上更改为
$_SESSION[\"cart_array\"] = array(\"item_id\" => $pid,\"quantity\" => 1);
这是未设置购物车数组或为空时运行的代码     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...