Tutorial :Serializing a checkbox array



Question:

long time reader/first time poster here.

So I've got a checkbox array that posted just fine to my table when I had an ajax post via:

var createListingString="&features=" + arrayCheckBox;  

Now I'm jquerying EVERYTHING (and loving it), but each time I try to post my array with data: $("#create_listing_1").serialize(), I just get "array" in the record it creates (instead of the actual values).

My checkboxes are all formatted:

<input type="checkbox" name="features[]" value="Non-smoking" /> Non-smoking <br />  

I'm sure that this is probably an easy one, but I'm making it difficult. AND I wanted to post my first question. Everyone here provides some amazing help, thanks for that.


Solution:1

You probably need to convert the array to a string first, PHP will give you the string 'Array' if it is converted implicitly. Although I'm not sure how it would have worked before, so apologies if I've completely misunderstood.

$string = implode(', ', $_POST['features']);  


Solution:2

I just had this same issue the other day. Here is how I resolved it:

var values = new Array();  $.each($("input[@name='features[]']:checked"), function() {      values.push($(this).val());  });  var createListingString = values.join();  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »