我的AJAX和PHP脚本都可以,它的功能应该是正常的 . 我唯一的问题是警报功能仅在双击按钮时触发 . 编辑:第一次单击时,网络选项卡显示应使用以下代码发送的所有数据:200

这是我发送给AJAX的代码

<input type="hidden" id="name<?=$product['id'];?>" name="hidden_name" 
value="<?=$product['title'];?>">

<input type="hidden" id="price<?=$product['id'];?>" name="hidden_price" 
class="hidden_price" value="<?=$product['price'];?>">

这是触发javascript代码的输入

<input type="button" class="button" name="cartbtn" id="<?=$product['id'];?>" 
value="Quick Add-to-Cart">

以下是将数据从输入发送到PHP脚本的javascript代码:

<script type="text/javascript">


 $(document).ready(function(data) {

        $('.button').click(function() {

                var product_id = $(this).attr('id');
                var product_name = $('#name' + product_id).val();
                var product_price = $('#price' + product_id).val();
                var action = "add";

                    console.log(product_id);


                    $.ajax({
                        url: "../PHP_Scripts/quick_cart.php",
                        method: "POST",
                        dataType: "json",
                        data: {

                            product_id: product_id,
                            product_name: product_name,
                            product_price: product_price,
                            action: action

                        },
                        success: function(data) {
                            $('#order_table').html(data.order_table);
                            $('.badge').text(data.cart_item);
                            alert("Procuct added to cart");
                        }
                    });
                });
            });
    </script>

任何人都可以了解这里发生的事情吗?我想要在单击按钮一次而不是两次按钮时将产品价格发送到购物车 .

HTTP代码是200;实际发送数据没有问题,只是数据只能通过双击而不是单击发送 . 我知道这一点,因为警报只会在双击时触发 .

编辑:这是我的购物车代码:

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 $key => $value) {

            if ($_SESSION['shopping_cart'][$key]['product_id'] == 
            $_POST['product_id']) {

                $is_available++;

                $_SESSION['shopping_cart'][$key]['product_quantity'] = 
                $_SESSION['shopping_cart'][$key]['product_quantity'] + 
                $_POST['product_quantity'];
            }

        }

        if ($is_available < 1) {

            $item_array = array(

                'product_id'        => $_POST['product_id'],
                'product_name'      => $_POST['product_name'],
                'product_price'     => $_POST['product_price'],
                'product_quantity'  => $_POST['product_quantity'],
                'product_image'     => $_POST['product_image']

            );

            $_SESSION['shopping_cart'][] = $item_array;

        }

    }

    else{

        $item_array = array(

                'product_id'        => $_POST['product_id'],
                'product_name'      => $_POST['product_name'],
                'product_price'     => $_POST['product_price'],
                'product_quantity'  => $_POST['product_quantity'],
                'product_image'     => $_POST['product_image']

            );

            $_SESSION['shopping_cart'][] = $item_array;

    }

    $order_table .= '

    <table class="table table-bordered">
        <tr style="color: #000;">
            <th width="40%">Product Name</th>
            <th width="10%">Quantity</th>
            <th width="20%">Price</th>
            <th width="15%">Total</th>
            <th width="5%">Action</th>
        </tr>


    ';

    if (!empty($_SESSION['shopping_cart'])) {

        $total = 0;


        foreach ($_SESSION['shopping_cart'] as $key => $value) {

            $order_table .= '

            <tr style="color: #000;">
                <td>'.$value['product_name'].'</td>
                <td>'.$value['product_quantity'].'</td>
                <td align="right">£'.$value['product_price'].'</td>
                <td align="right">£'.number_format($value['product_price'] * 
                 $value['product_quantity'], 2).'</td>
                <td><button name="delete" class="delete" 
                 id="'.$value['product_id'].'">Remove</button></td>
            </tr>

            ';

            $total = $total + ($value['product_price'] * 
            $value['product_quantity']);

        }

        $order_table .= '

        <tr style="color: #000;">
          <td colspan="3" align="right">Total</td>
          <td align="right">£'.number_format($total, 2).'</td>
          <td></td>
        </tr>

        ';

    }

    $order_table .= '</table>';
    $output = array(

        'order_table'     => $order_table,
        'cart_item'       => count($_SESSION['shopping_cart'])

    );
    echo json_encode($output);
 }
}

?>