Create a HTML form with PHP for loop and table

Posted by Shek on May 18, 2008 under PHP | 2 Comments to Read | Total View: 60,324 views

Editor’s note: To create a HTML form with just a few text fields and a few HTML form elements is easy, but in a stock control system or price input system for example, there might be a need for many text fields and different HTML form elements repeatedly appear in the HTML form. To type in the codes of all of them will take too much time. Therefore a PHP for loop and a table is good for this task.

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>

Not what you want? Try a Search

Add A Comment

*