I have some checkboxes to send to the server as array. Usually, I have the list of these checkboxes and I send it through an array[].
For example, submitting this :
<input type="checkbox" class="linkStyle" name="style[]" value="1" checked="checked" /> 1
<input type="checkbox" class="linkStyle" name="style[]" value="2" checked="checked" /> 2
<input type="checkbox" class="linkStyle" name="style[]" value="3" checked="checked" /> 3
i'll send to the server these parameters :
style[]=1
style[]=2
style[]=3
on server side, using PHP, I can get this array using $_POST['style'] (it put these parameters automatically into an array);
Now, doing it with an AJAX request, the scenario it's a bit strange, because using the function serialize() for example I don't get the same result : in fact, it put in a single variable the whole string, and it doesnt send theme values as distinct parameters.
Referring to the example before, it will send somethings like :
style: style%5B%5D=Style1&style%5B%5D=Style2&style%5B%5D=Style3
and this is uncomfortable on PHP, because it doesnt create an array for this values. So i've done my own script that emulate this behaviour. such as :
var styles="";
$('.linkStyle:checked').each(function(i,el){
styles=styles+'style[]='+$(this).val()+'&';
});
$.ajax ({
url: "list/browse_ajax.php",
type: "POST",
data: styles+'&id=browseUpdate&actualArtist='+browseArtist+'&actualEvent='+browseEvent+'&actualDate='+browseData,
success: function(data) {
$('#browseList').html(data);
},
error: function(data) {
$('#browseList').html("Error");
}
});
Now, what I'd like to know, is :
- What do you think about this strategy?
- Is it better create a JSON object and manage it on client/server side (memory, processing, timing, resources, and so on)?
- Why, for stuff like this, should I create an object, convert it as json, decode it on php, manage result, convert it on json and resend? I can manage just a string...
- Only PHP or others language server side (like ASP?) create automatically the array from the list of parameters with same name and []?
Hope that my question is enough understandable :)