Friday, April 26, 2013

Submitting Forms with multiple textboxes having same name

Creation of table with dynamic textboxes has been shown here. In such scenarios, it is good to name the textbox elements as "row[]" or "text[]" which look like an array construction. When the form is submitted, all the textbox's values will be available to PHP in an array which would be very easy for further processing.

Suppose, all the textboxes have a name structure like "text[]" as shown in the example form below.

<!-- FORM STARTS -->
<form name='f' method='post'>

<!-- INPUT Elements -->
<input type='text' name='text[]' value='0'>
<input type='text' name='text[]' value='1'>
<input type='text' name='text[]' value='2'>
<input type='text' name='text[]' value='3'>

<input type='submit' name='btn_submit' value='Submit Me'>

</form>
<!-- FORM ENDS -->


When we submit the form above, PHP receives values of all the textbox elements in an array format in variable $_POST['text']. If we run the following PHP statement :

<?php
print_r( $_POST['text'] );
?>


the output would be :

Array
(
    [text] => Array
        (
            [0] => 0
            [1] => 1
            [2] => 2
            [3] => 3
        )
)


If we give different index with the element as shown below :

<input type='text' name='text[123]' value='0'>
<input type='text' name='text[125]' value='1'>
<input type='text' name='text[147]' value='2'>
<input type='text' name='text[189]' value='3'>


the PHP  statement print_r( $_POST['text'] ); would give the following output.

Array
(
    [text] => Array
        (
            [123] => 0
            [125] => 1
            [147] => 2
            [189] => 3
        )
)


Creation of such textboxes can be done with the following piece of PHP code.

<table>
<?php
 for($i=0; $i<10; $i++)
 {
?>
  <tr>
    <td>
      <input type='text' name='text_<?php echo $i;?>'>
    </td>
  </tr>
<?php
}
?>
</table>


This generates the following HTML code. Notice the element's name is a bit different.

<table>
  <tr>
    <td> <input type='text' name='text_0'> </td>
  </tr>
  <tr>
    <td> <input type='text' name='text_1'> </td>
  </tr>
  <tr>
    <td> <input type='text' name='text_2'> </td>
  </tr>
  <tr>
    <td> <input type='text' name='text_3'> </td>
  </tr>
</table>


The statement :

<input type='text' name='text[<?php echo $i;?>]'>

would generate input elements like this :

<input type='text' name='text[0]'>
<input type='text' name='text[1]'>


But we can still manage to collect the element's value, after the form is submitted, easily.

<?php
for($i=0; $i<10; $i++)
{
  $index = "text_" . $i;
  echo $_POST[$index] . ", ";
}
?>


Now, a question ... what if all the elements share a common name as shown in the HTML below?

<input type='text' name='text' value='0'>
<input type='text' name='text' value='1'>
<input type='text' name='text' value='2'>
<input type='text' name='text' value='3'>


Only the last element's value will be available to PHP i.e $_POST['text'] would collect only last element's value of "3".

No comments: