I am trying to make a e-commerce filter system with PHP and AJAX. I have watched some guy on YouTube and it worked, but my e-commerce website is in the clothing industry and his was with electronics... So he did not explain how to filter sizes which is stored with quantity (for example: S:2, M:3, L:4) with the filter systems. Now if I add a filter for a product, the product shows that size correctly. But if I try it the second time, it does not execute. I think because of the way my column is set up in MySQL. Please help.
Here is my code for the filter system:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<ul class="list-group">
<li class="list-group-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" class="form-check-input product_check" value="S" id="Maat_Voorraad">S
</label>
</div>
</li>
<li class="list-group-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" class="form-check-input product_check" value="M" id="Maat_Voorraad">M
</label>
</div>
</li>
<li class="list-group-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" class="form-check-input product_check" value="L" id="Maat_Voorraad">L
</label>
</div>
</li>
<li class="list-group-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" class="form-check-input product_check" value="XL" id="Maat_Voorraad">XL
</label>
</div>
</li>
</ul>
<script type="text/javascript">
$(document).ready(function(){
$(".product_check").click(function(){
$("#loader").show();
var action = 'data';
var type = get_filter_text('type');
var maat_voorraad = get_filter_text('Maat_Voorraad');
$.ajax({
url:'action.php',
method:'POST',
data:{action:action,type:type,maat_voorraad:maat_voorraad},
success:function(response){
$("#result").html(response);
$("#loader").hide();
$("#textChange").text("Gefilterde producten");
}
});
});
function get_filter_text(text_id){
var filterData = [];
$('#'+text_id+':checked').each(function(){
filterData.push($(this).val());
});
return filterData;
}
});
</script>
// action.php
if(isset($_POST['maat_voorraad'])){
$maat_voorraad = implode("','", $_POST['maat_voorraad']);
$sql .= "AND Maat_Voorraad LIKE '%$maat_voorraad%'";
}
// Maat_Voorraad refers to my table column in database. There the size and quantity is
stored per shirt like: S:2, M:4, L:3
// maat_voorraad means size_and_quantity and that variable sends only the size
See examplehere I would really appreciate the help!