Jump to content

while statement handeling forms


daveh33

Recommended Posts

while ($row = mysql_fetch_array($result)) {
$time = $row['time'];
$tip = $row['tip'];
echo "<br /><form method=\"post\" action=\"\"><p align=\"center\">
<b>Tip</b><input type=\"text\" name=\"tip\" value=\"$tip\">   <b>Time</b><input type=\"text\" name=\"time\" value=\"$time\">
<br /><input type=\"submit\" name=\"submit\" value=\"submit\"></form>
<br />";

 

I have the above while statement - but how can I number the name fields instead of tip, as tip1, tip2, tip3 etc.. so they can be picked up as $_POST['tipx']

 

 

Link to comment
https://forums.phpfreaks.com/topic/87584-while-statement-handeling-forms/
Share on other sites

you could try this..

 

$tip_num=0;
while ($row = mysql_fetch_array($result)) {
$tip_num++;

$time = $row['time'];
$tip = $row['tip'];
echo "<br /><form method=\"post\" action=\"\"><p align=\"center\">
<b>Tip</b><input type=\"text\" name=\"tip{$tip_num}\" value=\"$tip\">   <b>Time</b><input type=\"text\" name=\"time\" value=\"$time\">
<br /><input type=\"submit\" name=\"submit\" value=\"submit\"></form>
<br />";

 

do you want more than one form? because how it looks now its going to echo the entire form code for every row

while ($row = mysql_fetch_array($result)) {
$time = $row['time'];
$tip = $row['tip'];
echo "<br /><form method=\"post\" action=\"\"><p align=\"center\">
<b>Tip</b><input type=\"text\" name=\"tip\" value=\"$tip\">   <b>Time</b><input type=\"text\" name=\"time\" value=\"$time\">
<br /><input type=\"submit\" name=\"submit\" value=\"submit\"></form>
<br />";

 

I have the above while statement - but how can I number the name fields instead of tip, as tip1, tip2, tip3 etc.. so they can be picked up as $_POST['tipx']

 

 

Use the following code instead:

<b>Tip</b><input type=\"text\" name=\"tip\" value=\"{$tip}[]\">   <b>Time</b><input type=\"text\" name=\"time\" value=\"$time\">

Ending a field name with [] will make the browser submit it as an array. So when you retrieve $_POST['tip'], it will hold an array rather than a string value. So $_POST['tip][0] will be the first field, $_POST['tip'][1] will be the second an so on.

 

OK - but do you mean name={$tip}[] & not the value?

Oops! O_o yeah I meant the name of the field not the value sorry, code corrected:

<b>Tip</b><input type=\"text\" name=\"tip[]\" value=\"$tip\">   <b>Time</b><input type=\"text\" name=\"time\" value=\"$time\">

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.