简体   繁体   English

div id在ajax成功时刷新

[英]div id refresh on ajax success

I have a div at index.php which i want to refresh as the ajax call succeeds. 我在index.php有一个div,我想在ajax调用成功时刷新。 I tried this. 我试过了 But it is not refreshing the div unless i refresh the whole page. 除非我刷新整个页面,否则它不会刷新div。 here is the code. 这是代码。

Div DIV

<div id="cartContainer">
    <div id="cart">
        <li style="color: #515151">
            <img id="cart_img" src="images/cart.png"> Cart <span class='badge' id='comparison-count'>
      <?php
      if(isset($_SESSION['cart'])&& !empty($_SESSION['cart']))
      {
        $cart_count=count($_SESSION['cart']);
        echo $cart_count;
      } else {
        $cart_count=0;
        echo $cart_count;
      }
        ?>
      </span>
        </li>
        <div id="sidebar">
            <?php if(isset($_SESSION[ 'cart'])&& !empty($_SESSION[ 'cart'])){ ?>
            <table id="s_table">
                <?php foreach($_SESSION[ 'cart'] as $id=> $value){ ?>
                <form class="product" method="get" action="index.php?">
                    <tr id="tr_s">

                        <input type="hidden" name="action" value="remove">
                        <input type="hidden" name="id" value="<?php echo $value['id'] ?>">
                        <td class="s_th">
                            <?php echo $value[ 'name']; ?>
                            <input type="hidden" name="name" value="<?php echo $value['name'] ?>">
                        </td>
                        <td class="s_th">
                            <?php echo $value[ 'quantity'] ?>
                            <input type="hidden" name="quantity" value="<?php echo $value['quantity'] ?>">
                        </td>
                        <td class="s_th">
                            <?php echo $value[ 'color']; ?>
                            <input type="hidden" name="color" value="<?php echo $value['color'] ?>">
                        </td>
                        <td class="s_th">
                            <?php echo $value[ 'size'] ?>
                            <input type="hidden" name="size" value="<?php echo $value['size'] ?>">
                        </td>

                        <td>
                            <button type="submit" id="btn_submit">Remove</button>
                        </td>
                    </tr>
                </form>
                <?php } ?>
                <tr>
                    <td class="cart_btn" colspan="2"><a href="index.php?page=cart" style="">GO TO CART</a>
                    </td>
                    <td class="cart_btn" colspan="2"><a href="index.php?page=checkout">CHECK OUT</a>
                    </td>
                </tr>
            </table>
            <?php } else { ?>
            <p id="p_s"><i> "Your Cart is Empty"</i>
            </p>
            <?php } ?>
        </div>
    </div>
</div>

Ajax 阿贾克斯

<script>
    $(document).ready(function() {
        $('#addtocart').click(function(e) {
            e.preventDefault();

            var page = $("#page").val(),
                action = $("#action").val(),
                name = $("#name").val(),
                id = $("#id").val(),
                color = $("#color").val(),
                size = $("#size").val(),
                cat_id = $("#cat_id").val(),
                s_cat_id = $("#s_cat_id").val(),
                category = $("#category").val();

            var proceed = true;
            if (proceed) {
                post_data = {
                    'Page': page,
                    'Action': action,
                    'Name': name,
                    'Cat_id': cat_id,
                    'S_cat_id': s_cat_id,
                    'Category': category,
                    'Id': id,
                    'Color': color,
                    'Size': size
                };
                $.post('add_cart.php', post_data, function(response) {

                    //load json data from server and output message
                    if (response.type == 'error') {
                        //output=$('.alert-error').html(response.text);
                    } else {
                        output = $("#cartContainer").load();
                    }

                    $(".alert-error").delay(3200).fadeOut(300);
                }, 'json');
            }
        });
    });
</script>

If you want to update the div with the return from you AJAX you would do something like this: 如果要使用AJAX的回报来更新div ,则可以执行以下操作:

 $.post('add_cart.php', post_data, function(response) {
     $("#cartContainer").html(response);
 }

Of course this depends on many things: the kind of data you're returning, what you wish to update, etc. It is hard to tell, from what you have posted, what your intent is. 当然,这取决于很多事情:您要返回的数据类型,您希望更新的数据等。从发布的内容很难看出您的意图。

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

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