I have a form with plenty of check-boxes. Check-boxes are divided to categories, each of which has about 20 check-boxes. Categories are size, color, etc... but to simplify this, I just explain the size category which has 20 check-boxes like this:
<form>
<input type="checkbox" name="size[]"><label class="checkboxvalue">size 1</label>
<input type="checkbox" name="size[]"><label class="checkboxvalue">size 2</label>
<input type="checkbox" name="size[]"><label class="checkboxvalue">size 3</label>
.
.
.
<input type="checkbox" name="size[]"><label class="checkboxvalue">size 20</label>
</form>
I want to Ajax call to the php script and retrieve the relating records from database which have the checked sizes (and of course the chosen color, pattern, design, ...).
I wondered if there is chance to send this many of data using arrays, because I have 10 categories and 20 check-boxes in each, resulting in a number of 200 parameters and I think this is too many.
If I could use an array it could be something like passing 10 arrays.
After hours of googling, I discovered that I should have my check-boxes like this <input type="checkbox" name="size[]">
instead of this <input type="checkbox" name="size1">
, but I am stuck in the JavaScript part. I used this:
<script>
$("input[name='size[]']").on( "click", function getValues() {
$("input[name='size[]']").map(function() {
return this.checked;
}).get().join(",");
});
</script>
This returns a list of comma-separated values for check-boxes like true,false,true,true,...,false,false
according to the size category check-boxes, but I don't know how to send them to the php script as an array. should I use the JSON formatting? how can I access them on php script? this is my ajax:
function ShowResult(size,color,pattern,design,application,producer) {
var xmlhttp;
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("main").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","filter.php?size[]="+size+"&color[]="+color+"&pattern[]="+pattern+"&design[]="+design+"&application[]="+application+"&producer[]="+producer,true);
xmlhttp.send();
}
As a complimentary portion of my question, what is the best way to minimize the transferred data to the script? is it good practice to send 0 or 1
instead of false or true
for the checked state of check-boxes in an array? Maybe, that's better to send just the index of those sizes with checked values. Is there a better way to accomplish this. Thanks in advance.