Variable variables – Get parameters of HTML elements which generated with a PHP for loop

Posted by Shek on November 30, 2011 under PHP | 3 Comments to Read | Total View: 24,906 views

This post is the continuation of the post “Create a HTML form with PHP for loop and table”. It mainly focus on how to catch the HTML elements’ data generated from a PHP of a HTML form, it also talks about PHP Variable variables.


In the previous post “Create a HTML form with PHP for loop and table”, it shows how to use a PHP for loop to create multiple HTML elements with different name, here in this post, I am going to show how to catch those form post elements’ values.

Based on the previous post example, we have a loop counter:

1
$numberofrow = 5;

and the code and example:

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>
1
2
3
4
5

In order to catch those elements’ data, we have to make use of the counter which is 5 in this example, Also

  • The textfield’s names are “textfield” with the counter number at the end, e.g. textfield1, textfield2, textfield3.
  • The drop down menus’ name are “select” with the counter number at the end, e.g. select1, select2, select3.

Therefore in the test.php file, we have:

1
2
3
4
5
6
7
8
9
10
11
<?php
$counter = 5;
 
$textfield = "textfield";
$select = "select";
 
for ($i = 1; $i <= $counter; $i++) {
 ${$textfield.$i} = $_POST["textfield$i"];
 ${$select.$i} = $_POST["select$i"];
} 
?>

Now we have created 10 variables to store the 10 $_POST variables from the HTML form, they are:

  • textfield1
  • textfield2
  • textfield3
  • textfield4
  • textfield5
  • select1
  • select2
  • select3
  • select4
  • select5

Further reference:

  1. Create a HTML form with PHP for loop and table
  2. PHP.net – Variable variables

    Not what you want? Try a Search

    • vikimanish said,

      hi friend …..my problem is when i click submit button last value which i entered in the text box only send to next page. i surfing half a day in google atlast i saw ur blog n got some idea. in my case i got textboxex by using while loop based upon the data i get from mysql database. plz help me…..

    • Shek said,

      Hey vikimanish,

      Please give more details on your problem. Is it you cannot pass the data of the textbox from the html page to the php file? Or the value cannot insert into the mysql?

      Shek

    • ansokclifford said,

      this is what im looking for.

      thanks for posting this one sir.

      cheers 🙂

    Add A Comment

    *