PHP form data into array


Home - Tutorials - Forms

In this small article I will show you how to send form data in case of multiple selection.

Tutorial info:


Name:PHP form data into array
Total steps:1
Category:Forms
Level:Beginner

Bookmark PHP form data into array



Bookmark and Share

Step 1 - PHP form with multiple selection


PHP form data into array

During PHP form processing it often occurs that the visitor can select multiple answers. It is in case of checkboxes, list- and combboxes. Let's suppose the following HTML form element:

Code:
  1. <SELECT NAME="color" SIZE=3 MULTIPLE>
  2. <OPTION>Green
  3. <OPTION>Black
  4. <OPTION>White
  5. </SELECT>

In this example the visitor can select more color at once. This is the result of the MULTIPLE parameter inside the SELECT tag. 

If you check the $_POST superglobal you will find all form variables in it. However if you check it more precisely you will find that the color variable contains only one of your selection. Where are the other selected values? Yes, they are missing in this case.

To solve this problem it would be nice if we would get an array with all of the user selection. Fortunately it is a quite simple task. You just need to edit the name parameter in your HTML form and add a [] after it as follows:

Code:
  1. <SELECT NAME="color[]" SIZE=3 MULTIPLE>
  2. <OPTION>Green
  3. <OPTION>Black
  4. <OPTION>White
  5. </SELECT>

Using this code you will get an array named color inside the $_POST array and this internal array contains all of the user selected values.

That's all. 







Tags: php form array, php form multiple selection, multiple selection form array

PHP form data into array - Table of contents
Step 1 - PHP form with multiple selection


Follow phpf1 on Twitter




F1 Site Family
AJAX F1
HTML F1
Java F1
JavaScript F1
PHP F1

Total time: 0.0535