PHP Ajax添加到购物车无法正常工作

时间:2017-11-25 02:21:21

标签: javascript php jquery ajax

您好我的添加到购物车按钮不能使用ajax,我无法看到问题所在。到目前为止,当我点击添加到购物车按钮时,没有发生任何事情,就像按钮没有调用ajax功能。你能帮助我吗?我是ajax的新手,我在互联网上的资源也无法帮助我。感谢

按钮

<input type="button" name="add_to_cart" id = "<?PHP echo $row["cardID"];?>" class="add_to_cart" value="Add to Cart" />

的Ajax

<script>
 $(document).ready(function(data){

  $('.add_to_cart').click(function(){
    var product_id = $(this).attr("cardID");
    var prodcut_name = $('#name' + product_id).val();
    var product_price = $('#price' + product_id).val();
    var product_quantity = $('#quantity' + product_id).val();
    var action = "add";
     if(product_quantity > 0){
       $.ajax({
       url="action.php",
       method:"POST",
       dataType:"json",
       data:{
          product_id:product_id,
          prodcut_name:prodcut_name,
          product_price:product_price,
          product_quantity:product_quantity,
          action:action
        },
        success:function(data)
        {
         $('#order_table').html(data.order_table);
         $('.badge').text(data.cart_item);
      alert("Product has been added to cart");
        }
    });
  }else{
    alert("Please Enter Number of Quantity");
  }
  });
  });
</script>

这是我的action.php

action.php的

<?php
session_start();
include_once('connection.php');
if(isset($_POST["product_id"]))
{
$order_table = '';
$message = '';
if($_POST["action"] == "add")
{
    if(isset($_SESSION["shopping_cart"]))
    {
        $is_available = 0;
        foreach($_SESSION["shopping_cart"] as $keys => $values)
        {
            if($_SESSION["shopping_cart"][$keys]['product_id'] == 
$_POST["product_id"])
            {
                $is_available++;
                $_SESSION["shopping_cart"][$keys]['product_quantity'] = 
$_SESSION["shopping_cart"][$keys]['product_quantity'] + $_POST["product_quantity"];
            }
        }
        if($is_available < 1)
        {
            $item_array = array(
                'product_id'            =>  $_POST["product_id"],
                'product_quantity'      =>  $_POST["product_quantity"],
                'product_name'          =>  $_POST["product_name"],
                'product_price'         =>  $_POST["[product_price"]
            );
            $_SESSION["shopping_cart"][] = $item_array;
        }
    }
    else
    {
        $item_array = array (
                'product_id'            =>  $_POST["product_id"],
                'product_quantity'      =>  $_POST["product_quantity"],
                'product_name'          =>  $_POST["product_name"],
                'product_price'         =>  $_POST["[product_price"]
        );
        $_SESSION["shopping_cart"][] = $item_array;
    }
    $order_table .= '
        <table class="table table-bordered">
        <tr>
            <th width = "20%">Quantity</th>
            <th width = "40%">Card</th>
            <th width = "20%">Price</th>
            <th width = "20%">Action</th>
        </tr>
    ';
    if(!empty($_SESSION["shopping_cart"]))
    {
        $total = 0;
        foreach($_SESSION["shopping_cart"] as $keys => $values)
        {
            $order_table .= '
                <tr>
                    <td>'.$values["product_quantity"].'</td>
                    <td>'.$values["product_name"].'</td>
                    <td align = "right">'.$values["product_price"].'</td>
                    <td><button name = "delete" class = "delete" id="'.$values["product_id"].'">&times;</button></td>
                </tr>
            ';
            $total = $total + ($values["product_quantity"] * $values["product_price"]);
        }
        $order_table .='
            <tr>
                <td colspan="3" align = "right">Total</td>
                <td align = "right">$ '.number_format($total, 2).'</td>
            </tr>
        ';
    }
    $order_table .= '</table>';
        $output = array(
            'order_table'       =>  $order_table,
            'cart_item'         =>  count($_SESSION["shopping_cart"])
        );
    }
    echo json_encode($output);

}
?>

先谢谢ajax的新人。

1 个答案:

答案 0 :(得分:1)

问题似乎在这一行

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

没有这样的属性。看来你想得到元素的id。用id

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