简体   繁体   English

Ajax成功功能仅更新首次点击的值

[英]Ajax success function only updating values for first click

Okay I have a html table given below: 好的,我在下面给出了一个html表:

<table class="table">
    <thead>
        <tr>
            <th>Product</th>
            <th>Quantity</th>
            <th colspan="2">Total</th>
        </tr>
    </thead>
    <tbody id="cart_table">
        ... 
    </tbody>
    <tfoot>
        <tr>
            <th colspan="2">Total</th>
            <th colspan="2">$446.00</th>
        </tr>
    </tfoot>
</table>

I am using ajax to append values clear tbody at every click of a button and refill the tbody with multidimensional session array. 我正在使用ajax在每次单击按钮时附加值清除tbody并用多维会话数组重新填充tbody。 It works fine for 1st turn, but does not work for 2nd click 第一回合效果很好,第二回合不起作用

My jquery code is given below 我的jQuery代码如下

 <script>
    $(document).ready(function(){
        $('.addToCart').on('click',function(e){
            $("#cart_table").empty();
            var price = $(this).attr('data-price');
            var item = $(this).attr('data-itemname');

                e.preventDefault();
                $.ajax({
                method : "POST",
                async: false,
                url: "./php/addToCart.php",
                dataType : "json",
                data : {item:item,price:price},
                success : function(response){
                  $("#cart_table").empty();
                  <?php 
                  $i = 0; 
                foreach ($_SESSION["cart_array"] as $each_item):
                $item = $each_item['item'];
                $price=$each_item['price'];
                $quantity=$each_item['quantity'];
                  ?>
                  $('#cart_table').append("<tr><td><?php echo $item; ?></td><td><?php echo $quantity; ?></td><td><?php echo $price; ?></td><td><a href='#'><i class='fa fa-trash-o'></i></a></td></tr>");
                <?php
                endforeach;
                ?>
                }

                });     

        });
    });
</script>

I have checked my session values are updated. 我已经检查了我的会话值是否已更新。 However, the change is not shown on my table except for first click. 但是,该更改未显示在我的桌子上,只有单击即可。

As pointed out in comments PHP is server side language, for it to reflect the changes, your page needs to reload but because you are using ajax page is not going to reload (Or rather we don't need it to reload). 正如评论中指出的那样,PHP是服务器端语言,为了反映更改,您的页面需要重新加载,但是因为您使用的是Ajax页面,所以不打算重新加载(或者,我们不需要重新加载它)。

It work for the first time because for the first time when page is loaded, the current data will already be present in your JavaScript function : 第一次使用是因为第一次加载页面时,当前数据已经存在于您的JavaScript函数中:

Right here : 就在这儿 :

$('#cart_table').append(<?php echo "<tr><td>".$item."</td><td>".$quantity."</td><td>".$price."</td><td><a href='#'><i class='fa fa-trash-o'></i></a></td></tr>";?>);

So what happens here is that when you click for the first time, your ajax will execute and success function is called, this function will add the already presented data because it will not update unless your page is reload. 因此,这里发生的是,当您第一次单击时,您的ajax将执行并调用成功函数,该函数将添加已经显示的数据,因为除非重新加载页面,否则它不会更新。 So every time you click, your session will be updated and success function is also called but your table will not reflect the changes. 因此,每次单击时,会话都会被更新,并且成功函数也会被调用,但是您的表不会反映所做的更改。

So, You need to change your PHP code To JavaScript, Here the basic idea of what to do, (Here i have assumed your response is JSON, And also note that following code will not work out of the box because i didn't tested it.) 因此,您需要将您的PHP代码更改为JavaScript,这是做什么的基本思想,(这里我假设您的响应是JSON,并且请注意,由于我未测试以下代码,因此无法立即使用它。)

<script>
$(document).ready(function(){
    $('.addToCart').on('click',function(e){
        $("#cart_table").empty();
        var price = $(this).attr('data-price');
        var item = $(this).attr('data-itemname');

            e.preventDefault();
            $.ajax({
            method : "POST",
            async: false,
            url: "./php/addToCart.php",
            dataType : "json",
            data : {item:item,price:price},
            success : function(response){
              $("#cart_table").empty();

              //New JavaScript code, make appropriate changes.
              jQuery.each(response, function(key, row) {
                  $('#cart_table').append("<tr><td>" + row.item + "</td><td>" + row.quantity + "</td><td>" + row.price + "</td><td><a href='#'><i class='fa fa-trash-o'></i></a></td></tr>");
              });
            }

            });     

    });
});

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

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