Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 成功的ajax请求时Div未更新(第一次请求时工作正常)_Php_Jquery_Ajax - Fatal编程技术网

Php 成功的ajax请求时Div未更新(第一次请求时工作正常)

Php 成功的ajax请求时Div未更新(第一次请求时工作正常),php,jquery,ajax,Php,Jquery,Ajax,我有一个header.php文件,其中有用于执行livesearch的代码。在那个搜索中,我直接加入了“添加到购物车”按钮,这样用户只需通过搜索就可以直接添加产品。 我的头文件中有Cart div,其中显示了购物车中的商品数量 使用“添加到购物车”按钮,Livesearch结果完全正常 当我第一次将任何产品添加到购物车时,这将替换头文件中购物车的值,并将“添加到购物车”按钮替换为“添加” 当我第二次使用相同的搜索结果将产品添加到购物车时,它会将按钮的值更新为“已添加”,但不会更新标题中购物车的值

我有一个header.php文件,其中有用于执行livesearch的代码。在那个搜索中,我直接加入了“添加到购物车”按钮,这样用户只需通过搜索就可以直接添加产品。 我的头文件中有Cart div,其中显示了购物车中的商品数量

  • 使用“添加到购物车”按钮,Livesearch结果完全正常
  • 当我第一次将任何产品添加到购物车时,这将替换头文件中购物车的值,并将“添加到购物车”按钮替换为“添加”
  • 当我第二次使用相同的搜索结果将产品添加到购物车时,它会将按钮的值更新为“已添加”,但不会更新标题中购物车的值。这就是问题所在
  • 这是我的密码:

    HEADER.PHP

    <script type="text/javascript">
    $(document).ready(function()
    {
    $(".searchproductbrand").keyup(function()
    {
    var kw = $(".searchproductbrand").val();
    if(kw != '')  
     {
      $.ajax
      ({
         type: "POST",
         url: "livesearch.php",
         data: "kw="+ kw,
         success: function(option)
         {
           $("#livesearch").show();
           $("#livesearch").html(option);
           document.getElementById("livesearch").style.border="1px solid #A5ACB2";
         }
      });
     }
     else
     {
       $("#livesearch").html("");
       document.getElementById("livesearch").style.border="0px";
     }
    return false;
    });
    
    $(document).delegate('.buynow','click', function(){
        var productid = $(this).attr('id');
        var quantity = $('#quantity_'+productid).val();
        var type= $('#type_'+productid).val();
        $.ajax({
            type: "POST",
            url: "db_addtocart.php",
            context: this,
            data: {quantity:quantity, 
                   type:type, 
                   productid:productid},
            success: function(option){
                   this.value = 'Added';
           $('#carttotal').empty();
           $('#carttotal').replaceWith(option);
            }
        });
        return false;
    });
    });
    </script>
    <div id='carttotal'><input type='button' class='button' id='cartdetails'
    value='<?php echo "Cart&nbsp(".$proInCart.")"?>' style='padding:7px;
    border-radius:15px;'></div>
    
    
    $(文档).ready(函数()
    {
    $(“.searchproductbrand”).keyup(函数()
    {
    var kw=$(“.searchproductbrand”).val();
    如果(千瓦!='')
    {
    $.ajax
    ({
    类型:“POST”,
    url:“livesearch.php”,
    数据:“kw=”+kw,
    成功:功能(选项)
    {
    $(“#livesearch”).show();
    $(“#livesearch”).html(可选);
    document.getElementById(“livesearch”).style.border=“1px solid#A5ACB2”;
    }
    });
    }
    其他的
    {
    $(“#livesearch”).html(“”);
    document.getElementById(“livesearch”).style.border=“0px”;
    }
    返回false;
    });
    $(document).delegate('.buynow','click',function(){
    var productid=$(this.attr('id');
    var数量=$('#数量'+productid).val();
    var type=$('#type#'+productid).val();
    $.ajax({
    类型:“POST”,
    url:“db_addtocart.php”,
    背景:这,,
    数据:{数量:数量,
    类型:类型,
    productid:productid},
    成功:功能(选项){
    this.value='添加';
    $('#cartotal').empty();
    $(“#carttotal”)。替换为(可选);
    }
    });
    返回false;
    });
    });
    
    在第一个请求之后,代码无法工作的原因是在函数结束时返回false。 您应该将函数更改为以下内容:

    <script type="text/javascript">
    $(document).ready(function()
    {
    $(".searchproductbrand").keyup(function(e)
    {
    var kw = $(".searchproductbrand").val();
    if(kw != '')  
     {
      $.ajax
      ({
         type: "POST",
         url: "livesearch.php",
         data: "kw="+ kw,
         success: function(option)
         {
           $("#livesearch").show();
           $("#livesearch").html(option);
           document.getElementById("livesearch").style.border="1px solid #A5ACB2";
         }
      });
     }
     else
     {
       $("#livesearch").html("");
       document.getElementById("livesearch").style.border="0px";
     }
     e.preventDefault();
    });
    
    $(document).delegate('.buynow','click', function(e){
        var productid = $(this).attr('id');
        var quantity = $('#quantity_'+productid).val();
        var type= $('#type_'+productid).val();
        $.ajax({
            type: "POST",
            url: "db_addtocart.php",
            context: this,
            data: {quantity:quantity, 
                   type:type, 
                   productid:productid},
            success: function(option){
                   this.value = 'Added';
           $('#carttotal').empty();
           $('#carttotal').replaceWith(option);
            }
        });
         e.preventDefault();
    });
    });
    </script>
    
    
    $(文档).ready(函数()
    {
    $(“.searchproductbrand”).keyup(函数(e)
    {
    var kw=$(“.searchproductbrand”).val();
    如果(千瓦!='')
    {
    $.ajax
    ({
    类型:“POST”,
    url:“livesearch.php”,
    数据:“kw=”+kw,
    成功:功能(选项)
    {
    $(“#livesearch”).show();
    $(“#livesearch”).html(可选);
    document.getElementById(“livesearch”).style.border=“1px solid#A5ACB2”;
    }
    });
    }
    其他的
    {
    $(“#livesearch”).html(“”);
    document.getElementById(“livesearch”).style.border=“0px”;
    }
    e、 预防默认值();
    });
    $(文档).delegate('.buynow','click',函数(e){
    var productid=$(this.attr('id');
    var数量=$('#数量'+productid).val();
    var type=$('#type#'+productid).val();
    $.ajax({
    类型:“POST”,
    url:“db_addtocart.php”,
    背景:这,,
    数据:{数量:数量,
    类型:类型,
    productid:productid},
    成功:功能(选项){
    this.value='添加';
    $('#cartotal').empty();
    $(“#carttotal”)。替换为(可选);
    }
    });
    e、 预防默认值();
    });
    });
    
    也许你可以试着做一个提琴,也许问题不在那里,在提琴上似乎工作得很好,为什么它在我的服务器上不工作?如果我使用$(“#XXXX”).append,这甚至对我也有效(我尝试在提琴上做的是因为缺少数据库),现在,empty函数&replaceWith有什么问题?empty从DOM中删除所选元素和子元素,而replaceWith用指定的新元素替换所选元素。是的,但这对我的代码有什么影响?我只是删除了元素,并用新元素替换了它。