简体   繁体   English

无法将产品添加到购物车

[英]Adding product to shopping cart not working

When I click on add to cart, It's not working although the button change from 'Add to cart' to 'Adding' meaning the jquery is working. 当我单击“添加到购物车”时,尽管按钮从“添加到购物车”更改为“正在添加”,这意味着jquery正在运行,但它不起作用。

I am creating an e-commerce with add to cart functionality but when I click on add to cart the products name is not added to the cart. 我正在创建具有添加到购物车功能的电子商务,但是当我单击添加到购物车时,产品名称未添加到购物车。 Although the button changes, meaning the json is working but the selection is not working. 尽管按钮更改,这意味着json在起作用,但是选择不起作用。

add produce in cart (manage_cart.php) 在购物车中添加农产品(manage_cart.php)

    if(isset($_POST["product_code"])) {
        foreach($_POST as $key => $value){
            $product[$key] = filter_var($value, FILTER_SANITIZE_STRING);
        }   
        $statement = $this->conn->prepare("SELECT product_name, product_price FROM products WHERE product_code=? LIMIT 1");
        $statement->bind_param('s', $product['product_code']);
        $statement->execute();
        $statement->bind_result($product_name, $product_price);
        while($statement->fetchAll()){ 
            $product["product_name"] = $product_name;
            $product["product_price"] = $product_price;     
            if(isset($_SESSION["produce"])){ 
                if(isset($_SESSION["produce"][$product['product_code']])) {             
                    $_SESSION["produce"][$product['product_code']]["product_qty"] = $_SESSION["produce"][$product['product_code']]["product_qty"] + $_POST["product_qty"];              
                } else {
                    $_SESSION["produce"][$product['product_code']] = $product;
                }           
            } else {
                $_SESSION["produce"][$product['product_code']] = $product;
            }   
        }   
        $total_product = count($_SESSION["produce"]);
        die(json_encode(array('produce'=>$total_product)));
    }

index.php 的index.php

     <?php
            $newstoreobj = $storeobj->select_product();

            if(count($newstoreobj)){
              foreach ($newstoreobj as $data) {
                ?>
                <form class="product-form">
                    <div class="col-md-3">
                        <a href="view_product/product.php?product_id=<?php echo  $data['product_id'] ?>/product<?php echo $data['product_name'].'.html'?>">
                            <div class="prods">
                                <div style="height:250px">
                                    <img src="upload/<?php echo $data['image1'];?>" class="img-responsive" style="padding:20px;height:100%;width:100%" />
                                </div>
                                <div>
                                    <p class="text-center" style="color:black; font-weight: bolder;"><?php echo $data['product_name'];?></p>
                                    <p class="text-center" style="color:red;font-weight:bold;font-size:24px">$<?php echo $data['product_price'];?>.00 </p>
                                </div>
                                </a>
                                <select name="product_qty" style="display: none;">
                                <option value="1" selected="">1</option>
                                <option value="2">2</option>
                                <option value="3">3</option>
                                <option value="4">4</option>
                                <option value="5">5</option>
                                </select>
                                <div class="buton" style="width:100;">

                                      <input name="product_code" type="hidden" value="<?php echo $data["product_code"];?>"> 
                                    <button type="submit" class="btn btn-block btn-danger">Add to Cart</button>
                                </div>
                            </div>


                    </div>
                     </form>
                    <?php
                }
            }
            ?>

    script.js

    // add item to cart
        $(".product-form").submit(function(e){
            var form_data = $(this).serialize();
            var button_content = $(this).find('button[type=submit]');
            button_content.html('Adding...'); 
            $.ajax({
                url: "manage_cart.php",
                type: "POST",
                dataType:"json",
                data: form_data
            }).done(function(data){         
                $("#cart-container").html(data.products);
                button_content.html('Add to Cart');             
            })
            e.preventDefault();
        }); 

when I click on the add to cart button, I want the product to be added to the cart. 当我单击“添加到购物车”按钮时,我希望将产品添加到购物车。 Thanks 谢谢

One of reason behind this is that you define die(json_encode(array('produce'=>$total_product))); 原因之一是您定义了die(json_encode(array('produce'=>$total_product))); in manage_cart.php manage_cart.php

It means result is stored in produce and when you get result in jquery using data.products . 这意味着结果存储在produce ,当您使用data.productsjquery获取结果时。

Thats why its not getting result. 那就是为什么它没有得到结果。

So please replace name in one place either in manage_cart.php or jQuery code 因此,请在manage_cart.phpjQuery代码中的一个位置替换名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM