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
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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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\">

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.