112

I have a form that is a little complex and I am hoping to simplify the server-side (PHP) processing by natively POSTing an array of tuples.

The first part of the form represents a User:

  • First Name
  • Last Name
  • Email
  • Address
  • etc

The second part of the form represents a Tree:

  • Fruit
  • Height
  • etc

The problem is that I need to be able to POST multiple Trees for a single User in the same form. I would like to send the information as a single User with an array of Trees but this might be too complex to do with a form. The only thing that comes to mind is using javascript to create some JSON message with a User object and an array of Tree objects. But it would be nice to avoid javascript to support more users (some people have scripts turned off).

lasec0203
  • 2,422
  • 1
  • 21
  • 36
styfle
  • 22,361
  • 27
  • 86
  • 128
  • 1
    Does the array guarantee order? I would like an array of Tree objects which would have multiple fields, so this would mean I have to use multiple arrays to represent a Tree and the arrays must be ordered so I can figure out which Tree they represent. – styfle Jan 31 '12 at 02:44

2 Answers2

193

check this one out.

<input type="text" name="firstname">
<input type="text" name="lastname">
<input type="text" name="email">
<input type="text" name="address">

<input type="text" name="tree[tree1][fruit]">
<input type="text" name="tree[tree1][height]">

<input type="text" name="tree[tree2][fruit]">
<input type="text" name="tree[tree2][height]">

<input type="text" name="tree[tree3][fruit]">
<input type="text" name="tree[tree3][height]">

it should end up like this in the $_POST[] array (PHP format for easy visualization)

$_POST[] = array(
    'firstname'=>'value',
    'lastname'=>'value',
    'email'=>'value',
    'address'=>'value',
    'tree' => array(
        'tree1'=>array(
            'fruit'=>'value',
            'height'=>'value'
        ),
        'tree2'=>array(
            'fruit'=>'value',
            'height'=>'value'
        ),
        'tree3'=>array(
            'fruit'=>'value',
            'height'=>'value'
        )
    )
)
SubjectDelta
  • 405
  • 1
  • 3
  • 14
Joseph
  • 117,725
  • 30
  • 181
  • 234
  • 6
    What chars are allowed in array indexing? Can we use ,.:` @? Example: `tree[tree 1][fr ui_t]` `tree[tree.1][fru:it]` `tree[tree@1][fru,it]` Is _ only safe delimiter in key naming? – CoR Jun 17 '14 at 18:53
  • 6
    would `tree[][fruit]` and `tree[][height]` not be valid to automatically index the array? – Wobbles Jul 07 '16 at 14:25
  • How do i get this submitted data as array/object in js to use in submit callback... i have tried `new formData($("form")[0])` and jQuery `$("form").serializeArray()` both returning `name="tree[tree1][fruit]"` as string....help – santoshe61 Mar 16 '20 at 10:08
  • Note: it doesn't work with jquery functions to collect form data as `.serialize()` or `.serializeArray()` – SubjectDelta Mar 18 '20 at 11:48
  • I tried but did not work for me in laravel – Veshraj Joshi Dec 17 '20 at 14:38
105

You can also post multiple inputs with the same name and have them save into an array by adding empty square brackets to the input name like this:

<input type="text" name="comment[]" value="comment1"/>
<input type="text" name="comment[]" value="comment2"/>
<input type="text" name="comment[]" value="comment3"/>
<input type="text" name="comment[]" value="comment4"/>

If you use php:

print_r($_POST['comment']) 

you will get this:

Array ( [0] => 'comment1' [1] => 'comment2' [2] => 'comment3' [3] => 'comment4' )
Dan
  • 1,154
  • 1
  • 7
  • 14