简体   繁体   English

如何在单击按钮时使用Ajax将数据发送到php

[英]How send data to php with ajax on button click

i have products on my website and i made ajax call to insert product to shopping cart, but there is problem with clicking button. 我的网站上有产品,我打电话给Ajax,将产品插入购物车,但是单击按钮有问题。 when i click on first product in list everything works but other product buttons do not react 当我单击列表中的第一个产品时,一切正常,但其他产品按钮不起作用

    <?php foreach ($products as $product):?>
       <?php if($product['kateg'] == "men"):?>       

<h5 style="color: #0669b4; min-height: 70px;"><?php echo $product['dasax']?></h5>
                                <p style="text-align: left; text-decoration: overline">Old price: <strong><span style="text-decoration: line-through"><?php echo round($product['fasi1'], 2)?> GEL</span></strong><br></p>
                                <p style="text-align: left">New Price: <strong><?php echo round($product['fasi2'], 2)?> GEL</strong><br></p>



        <input type="hidden" value="<?php echo $product['id']?>" id="product_id">
        <input type="hidden" value="<?php echo $product['dasax']?>" id="product_name">
        <input type="hidden" value="<?php echo $product['fasi2']?>" id="product_price">
        </a>
        <button class="btn btn-primary" type="submit" id="add_to_cart">Add</button>
        <?php endif;?>
    <?php endforeach;?>

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

        $('#add_to_cart').click(function () {
            var product_id = $("#product_id").val();
            var product_name = $("#product_name").val();
            var product_price = $("#product_price").val();

                $.ajax({
                    url: "/uketesi/index",
                    method: "POST",
                    dataType: "json",
                    data: {
                        product_id:product_id,
                        product_name:product_name,
                        product_price:product_price,
                    },
                    success:function (data) {
                      alert("produqti warmatebit daemata")
                $("#კალათა").html("<table id=\"example2\" class=\"table table-bordered table-hover\">" +
                    "<thead>" +
                    "<tr>" +
                    "<th>დასახელება</th>" +
                    "<th>ფასი</th>" +
                    "<th>იდენტიფიკატორი</th>" +
                    "</tr>" +
                    "<tr>" +
                    "<td>" + product_name + "</td>" +
                    "<td>" + product_price + "</td>" +
                    "<td>" + product_id + "</td>" +
                    "</tr>" +
                    "</thead>" +
                    "</table>");
                    }
                });
        });
    });

</script>

I figured out how to solve the problem above, i have another question how can i add multiple products with this code. 我想出了解决上述问题的方法,还有一个问题是如何使用此代码添加多个产品。 for now i only can add one product when i click on another product the old one dissepears. 现在,当我单击另一种旧产品时,我只能添加一种产品。

You can get the data from data-*="" attribute on the button. 您可以从按钮上的data-* =“”属性获取数据。 So no need hidden inputs. 因此,不需要隐藏的输入。 For the click event you should use class name. 对于click事件,您应该使用类名。 Other case ID wont be unique. 其他案例ID不会是唯一的。 Please try following code. 请尝试以下代码。

    <?php foreach ($products as $product):?>
       <?php if($product['kateg'] == "men"):?>       

<h5 style="color: #0669b4; min-height: 70px;"><?php echo $product['dasax']?></h5>
                                <p style="text-align: left; text-decoration: overline">Old price: <strong><span style="text-decoration: line-through"><?php echo round($product['fasi1'], 2)?> GEL</span></strong><br></p>
                                <p style="text-align: left">New Price: <strong><?php echo round($product['fasi2'], 2)?> GEL</strong><br></p>

        </a>
        <button class="btn btn-primary" type="submit" class="add_to_cart" data-id="<?php echo $product['id']?>" data-name="<?php echo $product['dasax']?>" data-price="<?php echo $product['fasi2']?>">Add</button>
        <?php endif;?>
    <?php endforeach;?>

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

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


            var product_id = $(this).data('id');
            var product_name = $(this).data('name');
            var product_price = $(this).data('price');

                $.ajax({
                    url: "/uketesi/index",
                    method: "POST",
                    dataType: "json",
                    data: {
                        product_id:product_id,
                        product_name:product_name,
                        product_price:product_price,
                    },
                    success:function (data) {

                    }
                });
        });
    });

</script>

Now that we have the form, and jQuery included in our document, we need to store it's values in 2 variables, ( val1 and val2 ) so then we can pass it to the PHP file to process it. 现在我们有了表单,并且文档中包含了jQuery,我们需要将其值存储在2个变量(val1和val2)中,然后才能将其传递给PHP文件进行处理。

$('#button').click(function() {
    var val1 = $('#text1').val();
    var val2 = $('#text2').val();
    $.ajax({
        type: 'POST',
        url: 'process.php',
        data: { text1: val1, text2: val2 },
        success: function(response) {
            $('#result').html(response);
        }
    });
});

As you can see in the code above, we create a .click event. 如您在上面的代码中看到的,我们创建一个.click事件。 This means that when the button with the ID of #button is click, out function will run. 这意味着当单击ID为#button的按钮时,输出功能将运行。 Then we get the value of each text field and store it in val1 and val2. 然后,我们获取每个文本字段的值并将其存储在val1和val2中。

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

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