Search database through multiple checkboxes using ajax

I found code on net to do this,but when i am implementing it returning me same result many times .
Can’t figure out where I am mistaking.
Here’s the code:

index.php

<form name="form1" id="search_form" method="POST">
<div class="totalinner">
<div class="categorysearchmain">
<div class="categorysearch">
<div class="categorysearch">
	<h3>Discount</h3>
	<ul>
			<li><input type="checkbox" value="10" class="sort_rang" name="size[]" ><p>10% and more</p><li>
			<li><input type="checkbox" value="20" class="sort_rang" name="size[]" <p>20% and more</p><li>
			<li><input type="checkbox" value="30" class="sort_rang" name="size[]" ><p>30% and more</p><li>
			<li><input type="checkbox" value="40" class="sort_rang" name="size[]" ><p>40% and more</p><li>
			<li><input type="checkbox" value="50" class="sort_rang" name="size[]" ><p>50% and more</p><li>
	</ul>
</div>
<div class="allproduct">
    <div class="allproducthead">
	     <h2 style="margin:0px;">Laptop</h2>
    </div>
	<div class="allproductinner" id="allproductinner">
	<?php
	 $db=new mysqli('localhost','root','','shopping');
	$sql="SELECT * FROM product";
    extract($_POST);
    if(isset($size)) 
        $sql.=" WHERE pdiscount IN (".implode(',', $size).")";
		
   $all_row=$db->query($sql);
	
	
	//while($row=mysqli_fetch_array($executequery))
	//{
?>
<?php if(isset($all_row) && is_object($all_row) && count($all_row)): $i=1;?>
    <?php foreach ($all_row as $key => $product) { ?> 
	 <div class="oneproducct">
		<img src="upload/<?php echo $product['pimage1']; ?>" >
		<h4><a href="productdetail.php?product_id=<?php echo $product['product_id'] ?>" style="text-decoration:none;"><?php echo $product['pname']; ?></a></h4>
		<!--<h2><del style="color:red;"><?php echo $product['pprice']; ?>Rs.</del></h2>-->
		<h2><?php echo $row['psellingprice']; ?>Rs.</h2>
		<a href="cart1.php?product=<?php echo $product['product_id']; ?>" style="text-decoration:none;"><input style="padding:5px;margin:10px 0px 10px 38px ;background-color:yellow;" type="submit" value="Add To Cart"></a>
	 </div>
<?php } ?>
<?php endif; ?>	 
<?php
//}
?>
<div class="clearfix"></div>	 
</div>
</div>	
<div class="clearfix"></div>
</div><!--totalinner-->
</form>

and ajax

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript">

$(document).on('change','.sort_rang',function(){
   var url = "ajax_search.php";
   $.ajax({
     type: "POST",
     url: url,
     data: $("#search_form").serialize(),
     success: function(data)
     {                  
        $('.oneproducct').html(data);
     }               
   });
  return false;
});
</script>

and ajax_search.php

<?php
    $db=new mysqli('localhost','root','','shopping');
   $sql="SELECT * FROM product";
    extract($_POST);
    if(isset($size)) 
        $sql.=" WHERE pdiscount IN (".implode(',', $size).")";
		
   $all_row=$db->query($sql);
?>
<?php if(isset($all_row) && is_object($all_row) && count($all_row)): $i=1;?>
    <?php foreach ($all_row as $key => $product) { ?>       
    <div class="oneproducct">
		<img src="upload/<?php echo $product['pimage1']; ?>" >
		<h4><a href="productdetail.php?product_id=<?php echo $product['product_id'] ?>" style="text-decoration:none;"><?php echo $product['pname']; ?></a></h4>
		<!--<h2><del style="color:red;"><?php echo $product['pprice']; ?>Rs.</del></h2>-->
		<h2><?php echo $product['psellingprice']; ?>Rs.</h2>
		<a href="cart1.php?product=<?php echo $product['product_id']; ?>" style="text-decoration:none;"><input style="padding:5px;margin:10px 0px 10px 38px ;background-color:yellow;" type="submit" value="Add To Cart"></a>
	 </div>
   <?php } ?>
<?php endif; ?>

although the code is working fine.but gives me repeated result.

Don’t you have to call fetchObject() to get each row returned by your query? I’m not familiar with mysqli, but example code seems to follow each ->query() with a while ($row = $result->fetchObject()) loop.

Also in your first code section, you have this line:

		<h2><?php echo $row['psellingprice']; ?>Rs.</h2>

but I don’t see where $row comes from.

i fix this. In this line

$('.oneproducct').html(data);

I was passing wrong div.It should be like this

 $('.allproductinner').html(data);

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.