четвъртък, 15 май 2008 г.

How to submit multiple form elements having the same name in PHP

In PHP, it is possible to have multiple form elements having the same name. However, the element's name must be appended with "[]".


<input name="hoho[]" value="first element" type="text">
<input name="hoho[]" value="second element" type="text">
<input name="hoho[]" value="third element" type="text">


At the processing PHP, the values of the submitted element can be retrieved as an array via:


$myarray = $_POST['hoho']; // Note that the name does not end with "[]" when retrieving
echo $myarray[0];
echo $myarray[1];
echo $myarray[2];

Няма коментари: