html textbox value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jakeneuman
Forum Newbie
Posts: 10
Joined: Mon Mar 01, 2010 3:12 am

html textbox value

Post by jakeneuman »

my problem on this code is that i could not get the value of each textbox displayed on this loop.
why i try to save in database nothing will save. anyone can help me on this?, thanks in advance

Code: Select all

//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>
davex
Forum Contributor
Posts: 101
Joined: Sat Feb 27, 2010 4:10 pm
Location: Namibia

Re: html textbox value

Post by davex »

Hi,

Well your code is a bit pseudo as it has PHP-style comments in the HTML sections. Anyway the following works ok:

Code: Select all

<form action=handler.php>
<?php
$rows = 5;
echo "<table>\n";
for ($counter=0; $counter<$rows; $counter++)
{
echo "<tr><td>".$counter."</td>\n";
echo "<td><input type=\"text\" name=\"textfield".$counter."\">";
echo "</td><td>";
echo "<select name=\"select".$counter."\">";
echo "<option value=1>1</option>";
echo "<option value=2>2</option>";
echo "</select></td></tr>\n";
}
echo "</table>";
?>
<input type="submit" value="go">
 
Which produces the HTML output:

Code: Select all

<form action=handler.php>
<table>
<tr><td>0</td>
<td><input type="text" name="textfield0"></td><td><select name="select0"><option value=1>1</option><option value=2>2</option></select></td></tr>
<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>
</table><input type="submit" value="go">
Which has your fields - note my loop starts at 0 though not one.

Your handler.php will then find the variables $_POST['textfield0'], $_POST['textfield1'] etc.

HTH.

Cheers,

Dave.
jakeneuman
Forum Newbie
Posts: 10
Joined: Mon Mar 01, 2010 3:12 am

Re: html textbox value

Post by jakeneuman »

my problem on this code is that i dont have fix number, it all depend on how many data from mysql table., i'll just used $numberofrow = 5 as a sample. and also my saving function is also in the same page, so i need to transfer those textbox value to a variable.
davex
Forum Contributor
Posts: 101
Joined: Sat Feb 27, 2010 4:10 pm
Location: Namibia

Re: html textbox value

Post by davex »

Ok just set $rows from $mysql_num_rows($query_result).

You'll also want to put somewhere in the form a hidden element with the number of rows so your handler knows how many rows are input (alternatively you could just loop until !isset($_POST['yourfield'+$increment) or something similar).

So...

Your display page would be something like:

Code: Select all

<form action=handler.php>
<?php
$result = mysql_query("SELECT * FROM sometable");
$rows = mysql_num_rows($result);
echo "<input type=\"hidden\" name=\"number_rows\" value=\"".$rows."\">";
echo "<table>\n";
for ($counter=0; $counter<$rows; $counter++)
{
$data = mysql_fetch_assoc($result); // this will get the row of the database...
echo "<tr><td>".$counter."</td>\n";
echo "<td><input type=\"text\" name=\"textfield".$counter."\" value=\"".$data['somerow']."\">";
echo "</td><td>";
echo "<select name=\"select".$counter."\">";
echo "<option value=1>1</option>";
echo "<option value=2>2</option>";
echo "</select></td></tr>\n";
}
echo "</table>";
mysql_free_result($result);
?>
<input type="submit" value="go">
And your handler.php would be something like:

Code: Select all

<?php
if (isset($_POST['number_rows']))
 {
 for ($i=0; $i<$_POST['number_rows']; $i++)
  {
  $textfield = $_POST['textfield'.$i];
  $selectfield = $_POST['select'.$i];
  // do whatever you want with $textfield and $selectfield which are the values for that value of $i
  }
 }
?>
Cheers,

Dave.
jakeneuman
Forum Newbie
Posts: 10
Joined: Mon Mar 01, 2010 3:12 am

Re: html textbox value

Post by jakeneuman »

thanks davex its working now :D.
davex
Forum Contributor
Posts: 101
Joined: Sat Feb 27, 2010 4:10 pm
Location: Namibia

Re: html textbox value

Post by davex »

No problem - glad to help.

Cheers,

Dave.
Post Reply