Ajax呼叫重复发布数据 [英] Ajax Call Repeat Post Data

查看:37
本文介绍了Ajax呼叫重复发布数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Ajax发布数据时遇到麻烦。我正在使用复选框,并将复选框的值发送到PHP页面。当我第一次单击查询时,我得到的查询是:

I am having trouble while posting data using Ajax. I am using checkboxes and send the checked box value to PHP page. While I first click then the query I Got is like:

SELECT * FROM `product` INNER JOIN `p_attributes` ON `product`.`product_id`=`p_attributes`.`product_id` WHERE `p_attributes`.`color_g` = 1  

但是当我第二次单击其他复选框而未取消选中第一个复选框时,得到此查询:

But when I second time click on other check box without unchecked the first one the I got this Query:

SELECT * FROM `product` INNER JOIN `p_attributes` ON `product`.`product_id`=`p_attributes`.`product_id` WHERE `p_attributes`.`color_g` = 1 AND `p_attributes`.`color_g` = 1 AND `p_attributes`.`color_r` = 1"  

p_attributes.color_g = 1 重复两次,这会影响整个查询数据。我只是发现我们必须停止/中止ajax请求以避免这种冲突,并且我已经尝试了许多在这里找到的已解决答案,但是没有任何效果,请有人提供解决方案摆脱这种问题。

The p_attributes.color_g = 1 repeats twice which effect the whole query data. I just found that we have to stop / abort the ajax request to avoid such confliction and I have already tried many solved answers found here but nothing is working. Please someone provide me the solution to get rid of such issue.

我的Ajax代码

var selected = new Array();
var size = new Array();
var url="<?php echo base_url('Cart/filt_color');?>";

$("input:checkbox[name=color]").click(function (){
    var currentRequest = null;
    // alert(url);
    $("input:checkbox[name=color]:checked").each(function() {
        selected.push($(this).val());
        //console.log(selected);
    });
    // Sizes 
    $("input:checkbox[name=size]:checked").each(function() {
        size.push($(this).val());
        //console.log(selected);
    });

    $.ajax({
        url:url,
        method:"post",
        data:{'colors':selected,'sizes':size},
        beforeSend : function()    {           
            if(currentRequest !== null) {
                currentRequest.abort();
                console.log("deleted");
            }
        },
        success:function(data) {
            // console.log(data);
            $("#mdv").html(data);
        }
    });
});

我的控制器代码

 function filt_color()
{
    $colors=$this->input->post('colors');
    $sizes=$this->input->post('sizes');
    if($colors !=='' || $sizes !=='')

    {

  $dataa= $this->crt->cfilter($colors,$sizes);


  if($dataa)
  {
      $output="";
      if(is_array($dataa))
      {
              foreach($dataa as $product){
             $output.='<div class="col-md-4 col-xs-6 product product-grid">
                <div class="product-item">
                  <div class="product-img">';

                    $output.='<a href="'.base_url().'">.';
                      $output.='<img src="'.base_url().'products/'.$product->product_image.'" alt="">';
                   $output.='</a>
                    <div class="product-label">
                      <span class="sale">sale</span>
                    </div>
                    <div class="product-actions">
                      <a href="http://deothemes.com/envato/gaze/html/shop-catalog.html#" class="product-add-to-compare" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Add to compare">
                        <i class="fa fa-exchange"></i>
                      </a>
                      <a href="http://deothemes.com/envato/gaze/html/shop-catalog.html#" class="product-add-to-wishlist" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Add to wishlist">
                        <i class="fa fa-heart"></i>
                      </a>                    
                    </div>';
                    $output.='<a href="'.base_url().'product/'.$product->product_id.'" class="product-quickview">Quick View</a>
                 </div>
                  <div class="product-details">
                    <h3>';
                     $output.='<a class="product-title" href="'.base_url().'product/'.$product->product_id.'">'.$product->product_name.'</a>
                   </h3>
                  <span class="price">
                      <del>'
                        .'<span>'. $product->product_price.'</span>'
                      .'</del>
                      <ins>
                        <span class="ammount">'.$product->product_price.'</span>
                     </ins>
                    </span>
                  </div>                                    

                </div>
              </div> <!-- end product -->';
              }
  }
  echo $output;

  }


    }
    else
  {
    echo "noting is available";


  }

}

我的模型代码

function cfilter($colors,$sizes)
    {


             $w='';$b='';$bl='';$r='';$g='';
             $sm='';$me='';$l='';$xl='';$xxl='';


            // Colors Foreach


         foreach($colors as $item) 
        {
           if(strpos($item, "bl" ) !== false ) 
           {               
            $bl=1;             
           }

           if(strpos($item, "bk" ) !== false )
           {
            $b=1;    
            $this->db->where('p_attributes.color_b',$b);
           }

           if(strpos($item, "r" ) !== false )
           {
            $r=1; 
            $this->db->where('p_attributes.color_r',$r);
           }
           if(strpos($item, "g" ) !== false )
           {
            $g=1; 
            $this->db->where('p_attributes.color_g',$g);
           }

           if(strpos($item, "w" ) !== false )
           {
           $w=1; 
           $this->db->where('p_attributes.color_w',$w);
           }
        }

        // Size Foreach

         foreach($sizes as $size) 
        {
           if(strpos($size, "sm" ) !== false ) 
           {

            $sm=1;  
            $this->db->where('p_attributes.size_sm',$sm);
           }
       if(strpos($size, "me" ) !== false )
           {
            $me=1;    
            $this->db->where('p_attributes.size_me',$me);
           }

           if(strpos($size, "l" ) !== false )
           {
            $l=1; 
            $this->db->where('p_attributes.size_l',$l);
           }

           if(strpos($size, "xl" ) !== false )
           {
            $xl=1; 
            $this->db->where('p_attributes.size_xl',$xl);
           }

           if(strpos($size, "xxl" ) !== false )
           {
           $xxl=1; 
           $this->db->where('p_attributes.size_xxl',$xxl);
           }

        }


        // DB Query
        $this->db->select('*');
        $this->db->from('product');
        $this->db->join('p_attributes', 'product.product_id=p_attributes.product_id','inner');
        $query = $this->db->get();
        var_dump($query);
        return $query->result();


    }
//       
    }


推荐答案

尝试将 var selected = new Array(); 放入 $(输入中: checkbox [name = color])这样的点击事件

Try to put the var selected = new Array(); into $("input:checkbox[name=color]") click event like this

$("input:checkbox[name=color]").click(function (){
    var selected = new Array();
    var currentRequest = null;
    // alert(url);
    $("input:checkbox[name=color]:checked").each(function() {
        selected.push($(this).val());
        //console.log(selected);
    });
    // Sizes 
    $("input:checkbox[name=size]:checked").each(function() {
        size.push($(this).val());
        //console.log(selected);
    });

    $.ajax({
        url:url,
        method:"post",
        data:{'colors':selected,'sizes':size},
        beforeSend : function()    {           
            if(currentRequest !== null) {
                currentRequest.abort();
                console.log("deleted");
            }
        },
        success:function(data) {
            // console.log(data);
            $("#mdv").html(data);
        }
    });
});

这篇关于Ajax呼叫重复发布数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆