I have search a lot but couldn't apply any answer to my problem.
I have a form generated by php. The form result is like this:
<form class="add-item">
<input type="checkbox" value="1" name="cust[]">
<input type="checkbox" value="2" name="cust[]">
<input type="checkbox" value="3" name="cust[]">
<input type="checkbox" value="4" name="cust[]">
<button class="submit">
</form>
This form can have 4 inputs or 20, depend the product.
So now I want to send the checked boxes to php through ajax. For this reason I have tried this:
$.ajaxSetup({
url: "testing_cart.php",
type: 'POST',
data: 'cust='+$('.add-item input[name="cust[]"]').serialize()+'',
success: function(data){
alert(data); // to see what is going on in php
},
error: function(){
alert('dio error');
}
});
$.ajax();
In my PHP file I have only this:
$cust = $_POST['cust'];
print_r($cust);// to see what ajax send
In some way this code is working but not as I am expecting.
If I check the only one checkbok I get this result:
cust[]='1' // the value of the checked checkbox
and if I check more than 1 I get an array but this array ignores the first item on the list... for example the code below is if I check all the checkbox... as you can see the first input is ignored:
Array
(
[0] => 2 // the value of the checked checkbox
[1] => 3 // the value of the checked checkbox
[2] => 4 // the value of the checked checkbox
)
I want to get an array always (if is possible) so if I the customer will select only 1 I get:
Array
(
[0] => 2 // the value of the checked checkbox
)
And if the customer select more well, all the values in an array.
Any idea?
p.s. sorry if my code vocabulary is not the best
JSONover php ?