Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to group $_POST variables?

Tags:

post

forms

php

I have a huge form (for an internal CMS) that is comprised by several sections, some of them are optional some of them are compulsory. All is under an humungous form (it has to be like this, no ajax, no other ways :-( )

Since in a Dilbertesque way everything get changed every second I was wondering if there is any simple way of grouping $_POST data, I mean sending POST like this:

$_POST['form1']['datax']

or to retrieve data from server side easily, and by easily I mean withouth having to expressily declare:

$array1 = array($_POST['datax'],$_POST['datay'],...);

$array2 = array($_POST['dataalpha'],$_POST['dataomega'],...);

since there are around 60 fields.

I hope I was able to explain this well and as always thank you very much..

like image 668
0plus1 Avatar asked Oct 16 '09 08:10

0plus1


1 Answers

If you give your input elements array-like names, they arrive in the PHP $_POST (or $_GET) array as an array:

<input type="text" name="foo[]" value="a"/>
<input type="text" name="foo[]" value="b" />
<input type="text" name="foo[]" value="c" />
<input type="text" name="foo[bar]" value="d" />
<input type="text" name="foo[baz][]" value="e" />
<input type="text" name="foo[baz][]" value="f" />

Goes to:

print_r($_POST)
foo => array (
    0 => a
    1 => b
    2 => c
    bar => d
    baz => array(
        0 => e
        1 => f
    )
)
like image 62
nickf Avatar answered Oct 15 '22 02:10

nickf