Create a HTML form with PHP for loop and table
I have create an example to show how to a HTML form with PHP for loop, I called it “testform.php”.
First we have to create the html form and a table:
//create form <form name="aform" action="test.php" method="post"> //create table <table border="2"> ........ </table> </form>
Then inside, we have to create a PHP variable to store an integer that acts as a row counter of HTML elements which you want the number of row to have in the HTML form, I set it to 5 for this example:
//create form <form name="aform" action="test.php" method="post"> //create table <table border="2"> //create the row counter <?php $numberofrow = 5;?> ........ </table> </form>
Now we create the for loop for repeating the rows and the HTML elements inside:
//create form <form name="aform" action="test.php" method="post"> //create table <table border="2"> //create the row counter <?php $numberofrow = 5;?> //create the for loop <?php for($counter = 1;$counter<=$numberofrow;$counter++){ ?> ........ <?php }?> </table> </form>
Then we create just 1 row in it for the repeating process:
//create form <form name="aform" action="test.php" method="post"> //create table <table border="2"> //create the row counter <?php $numberofrow = 5;?> //create the for loop <?php for($counter = 1;$counter<=$numberofrow;$counter++){ ?> //create 1 row for repeating <tr> ........ </tr> <?php }?> </table> </form>
Now, we put the HTML elements inside the columns of the row. I have placed a text field and a drop down menu, therefore 2 columns used in this case.
//create form <form name="aform" action="test.php" method="post"> //create table <table border="2"> //create the row counter <?php $numberofrow = 5;?> //create the for loop <?php for($counter = 1;$counter<=$numberofrow;$counter++){ ?> //create 1 row for repeating <tr> //column 1 is to print out the counter for you to see. <td><?php echo $counter; ?></td> /*column 2 is a text field and the name is "textfield"+the value of the counter, therefore they can have different names.*/ <td> <input type="text" name="textfield<?php echo $counter;?>" /></td> /*column 2 is a drop down menu and the name is "select"+the value of the counter, therefore they can have different names.*/ <td> <select name="select<?php echo $counter;?>"> <option value="1">1</option> <option value="2">2</option> </select> </td> </tr> <?php }?> </table> </form>
At last I placed a submit button outside the for loop because we only need 1 submit button.
//create form <form name="aform" action="test.php" method="post"> //create table <table border="2"> //create the row counter <?php $numberofrow = 5;?> //create the for loop <?php for($counter = 1;$counter<=$numberofrow;$counter++){ ?> //create 1 row for repeating <tr> //column 1 is to print out the counter for you to see. <td><?php echo $counter; ?></td> /*column 2 is a text field and the name is "textfield"+the value of the counter, therefore they can have different names.*/ <td> <input type="text" name="textfield<?php echo $counter;?>" /></td> /*column 2 is a drop down menu and the name is "select"+the value of the counter, therefore they can have different names.*/ <td> <select name="select<?php echo $counter;?>"> <option value="1">1</option> <option value="2">2</option> </select> </td> </tr> <?php }?> //create the submit button <tr> <td> <input type="submit" name="Submit" value="submit"/></td> </tr> </table> </form>
This is the actual HTML form:
| 1 | ||
| 2 | ||
| 3 | ||
| 4 | ||
| 5 | ||
If you want more rows with the same HTML form elements but different names, you just change the $numberofrow, if you want 1000 then $numberofrow=1000;
If you do not use a for loop, you have to type in all this:
<form name="aform" action="test.php" method="post"> <table border="2"> <tr> <td>1</td> <td> <input type="text" name="textfield1" /></td> <td> <select name="select1"> <option value="1">1</option> <option value="2">2</option> </select> </td> </tr> <tr> <td>2</td> <td> <input type="text" name="textfield2" /></td> <td> <select name="select2"> <option value="1">1</option> <option value="2">2</option> </select> </td> </tr> <tr> <td>3</td> <td> <input type="text" name="textfield3" /></td> <td> <select name="select3"> <option value="1">1</option> <option value="2">2</option> </select> </td> </tr> <tr> <td>4</td> <td> <input type="text" name="textfield4" /></td> <td> <select name="select4"> <option value="1">1</option> <option value="2">2</option> </select> </td> </tr> <tr> <td>5</td> <td> <input type="text" name="textfield5" /></td> <td> <select name="select5"> <option value="1">1</option> <option value="2">2</option> </select> </td> </tr> <tr> <td> <input type="submit" name="Submit" value="submit"/></td> </tr> </table> </form>





