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:
1 2 3 4 5 6 7 | //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:
1 2 3 4 5 6 7 8 9 | //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:
1 2 3 4 5 6 7 8 9 10 11 12 | //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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | //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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | //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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | //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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | <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> |
Similar Posts:
- Variable variables – Get parameters of HTML elements which generated with a PHP for loop
- HTML form in a table
- Display all field (column) names of a table in MySQL database
- Using XML elements as HTML form drop down menu items with PHP
- HTML form – Drop down menu with data from MySQL datebase as options





Alain said,
Hi,
I'm using the same logic with a while but my problem is the test.php that I don't see here. How do you get the data submitted?
Shek said,
Hey Alain,
Thank you for coming to my blog.
Please refer to my latest post:
http://www.phpsuperblog.com/php/variable-variables-get-parameters-of-html-elements-which-generated-with-a-php-for-loop/
for reference.
Cheers!!
Add A Comment