AJAX multiple checkboxes / multiselect

Alrighty then, howdy first off; quick question I have a form that has multiple check boxes with the same name (i.e. -

<input type="checkbox" name="myname[]" value="1" />
<input type="checkbox" name="myname[]" value="1" />
<input type="checkbox" name="myname[]" value="2" />
<input type="checkbox" name="myname[]" value="3" />
<input type="checkbox" name="myname[]" value="4" />

)

I am not using JQuery as I don't need that much baggage it is a very simple script that does does what is says on the tin, just serialize a form for use in an ajax post request. I would like to know though how to serialize the multiple checkboxes above for correct use in php.

Answers


I think the easiest way would be to grab all the checkboxes with document.getElementsByName and loop to get what's checked.

var checkedValues = [];
var allCheckboxes = document.getElementsByName("myname[]");
for(var i = 0; i < allCheckboxes.length; i++){
    if (allCheckboxes[i].checked)
       checkedValues.push(allCheckboxes[i].value);
}

I assume PHP can treat a comma-delimited list of values as an array:

checkedValues.join();   //tested and produces ---> 0,3,4

Need Your Help

What is the difference between not initializing a pointer, and having it be initialized to null?

c++ struct null initialization declare

I'm building a simple generic engine for my true start in the making of games, and I am trying to be somehow organized and decent in the making of my engine, meaning I don't want it to be something I

php include file in array

php arrays include file-get-contents

I am trying to create an array that should return some html as well of some other data through an ajax call. The thing is I need to include a php file that first has to build the HTML page, bue whe...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.