Store form text input field data in an array in PHP

Description

The following code shows how to store form text input field data in an array.

Example


<HTML>//from   w w  w  . j  ava  2s  .co  m
<BODY>
<FORM METHOD="POST" ACTION="index.php">
<H1>Contact Information</H1>
<TABLE>
<TR>
  <TD>Childrens' Names:</TD>
</TR>
<TR>
  <TD><INPUT TYPE="TEXT" NAME="children[]"></TD>
</TR>
<TR>
  <TD><INPUT TYPE="TEXT" NAME="children[]"></TD>
</TR>
<TR>
  <TD><INPUT TYPE="TEXT" NAME="children[]"></TD>
</TR>
<TR>
  <TD><INPUT TYPE="TEXT" NAME="children[]"></TD>
</TR>
<TR>
  <TD><INPUT TYPE="TEXT" NAME="children[]"></TD>
</TR>
</TABLE>
<BR>
<BR>
<BR>
<INPUT TYPE="SUBMIT" VALUE="Submit">
<BR>
<BR>
<INPUT TYPE="RESET"  VALUE="Clear the Form">
</FORM>
</BODY>
</HTML>

<!-- index.php

<?php
foreach ($children as $index => $child){
    echo "<BR>child[$index]=$child";
}
echo "<BR>";

sort($children);

foreach ($children as $index => $child){
    echo "<BR>child[$index]=$child";
}
?>
-->

The code above generates the following result.

Store form text input field data in an array in PHP




















Home »
  PHP Tutorial »
    Form »




PHP Form
Form Demo