Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to post multiple <input type="checkbox" /> as array in PHP?

Tags:

So that in PHP I can deal with them as :

foreach($_POST['checkboxname'] as $i => $value) ... 
like image 506
user198729 Avatar asked Dec 30 '09 06:12

user198729


People also ask

How can I store multiple checkbox values in an array in PHP?

Add square brackets ( [] ) at the end of the checkbox name when a form has multiple checkboxes with the same name. PHP creates an associative array to stored values of the selected checkboxes if checkboxes have the same name that ends with [] .

How do I make an array of checkboxes in HTML?

To link several checkboxes together to make them into an array in the PHP $_POST array you need to make all of the checkboxes have the same name, and each name must end in "[]". When both of the checkboxes are filled and the form is submitted this produces the following array.


2 Answers

Do something like this:

<input type="checkbox" name="checkboxArray[]" /> 

Note the [] in the name.

like image 134
KramerC Avatar answered Nov 09 '22 05:11

KramerC


Like this:

<input type="checkbox" name="checkboxname[]" /> <input type="checkbox" name="checkboxname[]" /> <input type="checkbox" name="checkboxname[]" /> <input type="checkbox" name="checkboxname[]" /> <input type="checkbox" name="checkboxname[]" /> 

Just append [] to their names.

like image 44
Sarfraz Avatar answered Nov 09 '22 05:11

Sarfraz