Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将AJAX功能实现到一个篮子中_Php_Jquery_Ajax - Fatal编程技术网

Php 将AJAX功能实现到一个篮子中

Php 将AJAX功能实现到一个篮子中,php,jquery,ajax,Php,Jquery,Ajax,从Jquery中,我从addtocart按钮获得了产品ID。每当我单击该按钮时,都会发送ajax代码以获取cart.php文件。我假设AJAX类型是一个GET请求,用于在cart.php中发送此代码?我已检查控制台,但似乎未执行任何操作:?我没有包含获取$row['product_id']的PHP代码,因为这不是问题所在 cart函数-quantity details:我在basket.php中调用这个函数 function cart() { //die(print_r($_SESSI

从Jquery中,我从addtocart按钮获得了产品ID。每当我单击该按钮时,都会发送ajax代码以获取cart.php文件。我假设AJAX类型是一个GET请求,用于在cart.php中发送此代码?我已检查控制台,但似乎未执行任何操作:?我没有包含获取$row['product_id']的PHP代码,因为这不是问题所在

cart函数-quantity details:我在basket.php中调用这个函数

function cart()
{

    //die(print_r($_SESSION));
    $total         = 0;
    $item_quantity = 0;
    $item_name     = 1;
    $item_number   = 1;
    $amount        = 1;
    $quantity      = 1;
    $shipping_cart = 3.50;

    foreach ( $_SESSION as $name => $value ) {

        if ( $value > 0 ) {

            if ( substr($name, 0, 8) == "product_" ) {
                $length = strlen($name);
                $id     = substr($name, 8, $length);
                if ( $id != 'id' ) { // This is ADDED.
                    $query = query("SELECT * FROM products WHERE product_id = " . $id . " ");

                    confirm($query);


                    while ( $row = fetch_array($query) ) {


                        $item_quantity += $value;


                        $product_image = $row['product_image'];
                        $product_price = $row['product_price'];

                        $sub = $product_price * $value;

                        $product = <<<DELIMETER
<tr>
  <td>{$row['product_title']}<br>
  <img width='100' src='assets/images/{$product_image}'>
  </td>
  <td>&pound;{$product_price}</td>
  <td>{$value}</td>
  <td>&pound;{$sub}</td>
  <td><a class='btn btn-warning' href="cart.php?remove={$row['product_id']}"><span class='glyphicon glyphicon-minus'></span></a>   <a class='btn btn-success' href="cart.php?add={$row['product_id']}"><span class='glyphicon glyphicon-plus'></span></a>
<a class='btn btn-danger' href="cart.php?delete={$row['product_id']}"><span class='glyphicon glyphicon-remove'></span></a></td>        
  </tr>
<input type="hidden" name="item_name_{$item_name}" value="{$row['product_title']}">
<input type="hidden" name="item_number_{$item_number}" value="{$row['product_id']}">
<input type="hidden" name="amount_{$amount}" value="{$row['product_price']}">
<input type="hidden" name="quantity_{$quantity}" value="{$value}">
DELIMETER;

                        echo $product;

                        $item_name++;

                        $item_number++;

                        $amount++;

                        $quantity++;
                    }

                    $fee = '5';

                    $_SESSION['item_total'] = $total += $sub;

                    $_SESSION['item_quantity'] = $item_quantity;

                   if($_SESSION['item_total'] > '100'){

                        $_SESSION['item_total'];

                   }

                    else{



                         $_SESSION['item_total'] += $fee;

                    }



                } 



        } 
    }


}

}


?>
功能购物车()
{
//模具(打印($会话));
$total=0;
$item_数量=0;
$item_name=1;
$item_number=1;
$amount=1;
$quantity=1;
$shipping_cart=3.50;
foreach($\会话为$name=>$value){
如果($value>0){
if(substr($name,0,8)=“产品”){
$length=strlen($name);
$id=substr($name,8,$length);
如果($id!='id'){//,则添加此项。
$query=query(“从产品中选择*,其中product_id=“.$id.”);
确认($query);
while($row=fetch_数组($query)){
$item\u数量+=$value;
$product_image=$row['product_image'];
$product_price=$row['product_price'];
$sub=$product\u price*$value;

$product=那么什么不起作用了?请向我们展示服务器端的PHP代码。我已经展示了PHP代码-它在脚本标记下面。请参见这里:
数据:“id=“+product\u id,
,它应该是
数据:“add=”+product_id,
。另外,您必须在
WHERE
子句中将字符串用单引号括起来。@Rajdeep Paul更改数据现在可以工作了,它添加到basket而不刷新。您能告诉我如何在标题中的cart链接旁边显示product_数量而不刷新basket.php页面吗?就像每次我带项目a进入basket.php一样dded标题中的数量为零。我已在问题后添加了此代码。从后端页面删除
重定向();
语句。在购物车中添加产品后,使用
成功(){}
回调函数操作行,即将+1添加到产品数量并更新产品行。
 <li style="text-decoration:none; font-size:16px; list-style-type:none; display:inline-block;"><a href="basket.php" id="cart" style='color:#fff;'><i class="fa fa-shopping-cart"></i> Basket <span id="qty" class="badge"></span></a></li>
 <a href="cart.php?add=<?php echo $row['product_id']; ?>" class="btn btn-success btn-lg Prodadd" id="<?php echo $row['product_id']; ?>" style="border-radius:2px;color:#fff;">Add to basket</a>



<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<script type="text/javascript">

    $(document).ready(function(){

        $('.Prodadd').on("click", function(){



     var qty = '<?php 
echo isset($_SESSION['item_quantity']) ? $_SESSION['item_quantity'] : $_SESSION['item_quantity'] = "0";?>';

            var product_id = $(this).attr("id");



            $.ajax({

                type: "GET",
                url: "cart.php",
                data: "add":product_id,
                success: function(){

                    alert('added to basket');
                    $('#qty').html(qty);


                }


            });

            return false;



        });


                   });

    </script>
<?php require_once("includes/db.php"); ?>

<?php


if (isset($_GET['add']) ) {

    $query = query("SELECT * FROM products WHERE product_id=" . escape_string($_GET['add']) . " ");

    confirm($query);

    while ($row = fetch_array($query) ) {

        if ($row['product_qty'] != $_SESSION[ 'product_' . $_GET['add'] ] ) {

            $_SESSION['product_' . $_GET['add'] ] += 1;

            redirect("basket.php");


        } else {

            set_message3("We only have " . $row['product_qty'] . " " . "items" . " " . "of" . " " . $row['product_title'] . " " . "left in stock");

            redirect("basket.php");
        }

    }

}

?>