Ajax调用重复发布数据

时间:2018-04-17 12:05:49

标签: php ajax codeigniter codeigniter-3

使用Ajax发布数据时遇到问题。我正在使用复选框并将复选框值发送到PHP页面。当我第一次点击时,我得到的查询就像:

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

但是当我第二次点击其他复选框而没有取消选中第一个时,我得到了这个查询:

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请求以避免这种冲突,我已经尝试了很多解决的答案,但是没有任何工作。请有人为我解决这个问题。

我的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();


    }
//       
    }

1 个答案:

答案 0 :(得分:0)

尝试将var selected = new Array();置于$("input:checkbox[name=color]")点击事件中

$("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);
        }
    });
});