×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Retrieving Data

Dealing with HTML form input when the form has a variable number of fields by sleipnir214
Posted: 6 Feb 03

PHP has an undocumented feature where if a set of forms have names that read like arrays, PHP will treat that input as an array.

For example, suppose your HTML form reads as follows:

<html><body><form method=POST action=test_arrayinput.php>
    <input type=text name="foo[1]">
    <input type=text name="bar[1]"><br/>
    <input type=text name="foo[2]">
    <input type=text name="bar[2]">
    <input type=submit>
</form></body></html>


If you enter data in those fields and submit it to test_arrayinput.php, in that script $_POST will be in a state like:

Array
(
    [foo] => Array
        (
            [1] => a
            [2] => b
        )
    [bar] => Array
        (
            [1] => 1
            [2] => 2
        )
)


Notes:
1.  The array subscripts specified do not have to be numerical -- you can use strings as well.  In that case $_POST['foo'] would be an associative array.

2.  You don't have to specify indices at all in the names of your fields -- "foo[]" will work, too.  This can be useful in cases where you don't care to maintain any relationships between fields.  To collect input from a set of checkboxes, for example.

3.  Using array subscripted field names really messes up JavaScript.  If you need to reference the values of the fields in client-side JavaScript, you will not be able to using field names -- you'll have to use the JavaScript field arrays.

Back to PHP FAQ Index
Back to PHP Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close