Passing Arrays in Forms PHP

Though it may not be apparent, it is possible to pass arrays from a form. To understand how, you must recall how form fields are turned into PHP variables. Each field is read in order by PHP and turned into an assignment statement.

The name of the form field is treated as the left side of an assignment statement. This means that if other special characters appear as part of the name of the field, they will interpreted accordingly. You can include square brackets to force the variable to be an array. An empty pair of square brackets will add a value to an array using consecutive integers. So, if you name multiple fields in a form with the same name that ends in a pair of empty brackets, an array will be constructed for you when the form is submitted.

There are limitations to this technique. Only single-dimension arrays are passed correctly. You also need to be aware of buggy browsers. You should always place double quotes around field names anyway, but I've run into browsers that don't pass fields properly when fields ending in square brackets aren't quoted.

Passing an Array via a Form

Passing an Array via a Form

Passing an Array via a Form

Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

PHP Topics