By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,412 Members | 2,882 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,412 IT Pros & Developers. It's quick & easy.

Form inside loop, need to wait till submit is activated.

P: n/a
I have the following.

$result=mysql_query($sql);
$nrows=mysql_num_rows($result);
for ($i=0;$i<$nrows;$i++)
{
$row_array=mysql_fetch_row($result);
echo "<form name='testform' action='ins_op.php' method='post'>";

lots of form stuff

echo "<td><input type='submit' value='Save'>";

echo "</form>";
}

I'd like to have the loop wait until the form submit is pressed before
it continues and grabs the next record from $row_array.

Right now the code displays all the records with their submit buttons.

Doing it in this form permits one query to the database to get all the
open records.

Alternative code structures would be entertained.

thanks

Sep 12 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
It is amazing how clear errors in code logic are soooo clear when
posted on world wide bulletin boards.
As the code is written submiting the form will close the current
program and open the program in the form action.
So a form can not be the right way to tackle this.
Unless someone can come with a more elegant method, I guess I am going
to brute force this.

Sep 12 '05 #2

P: n/a

<rj***********@gmail.com> wrote in message
news:11********************@g44g2000cwa.googlegrou ps.com...
I have the following.

$result=mysql_query($sql);
$nrows=mysql_num_rows($result);
for ($i=0;$i<$nrows;$i++)
{
$row_array=mysql_fetch_row($result);
echo "<form name='testform' action='ins_op.php' method='post'>";

lots of form stuff

echo "<td><input type='submit' value='Save'>";

echo "</form>";
}

I'd like to have the loop wait until the form submit is pressed before
it continues and grabs the next record from $row_array.

Right now the code displays all the records with their submit buttons.

Doing it in this form permits one query to the database to get all the
open records.


why do you want to do this ? you can get all the data in one query
anyway...
Sep 12 '05 #3

P: n/a
Yeah, nix this query.
After reviewing the code, I am revisiting the logic flow from top down.

Sep 12 '05 #4

P: n/a
In article <11********************@g44g2000cwa.googlegroups.c om>,
rj***********@gmail.com wrote:
I have the following.

$result=mysql_query($sql);
$nrows=mysql_num_rows($result);
for ($i=0;$i<$nrows;$i++)
{
$row_array=mysql_fetch_row($result);
echo "<form name='testform' action='ins_op.php' method='post'>";

lots of form stuff

echo "<td><input type='submit' value='Save'>";

echo "</form>";
}

I'd like to have the loop wait until the form submit is pressed before
it continues and grabs the next record from $row_array.

Right now the code displays all the records with their submit buttons.

Doing it in this form permits one query to the database to get all the
open records.

Alternative code structures would be entertained.

thanks

I am guessing that you have a table of values and you want to be able to edit
those values one by one until every row has been edited? Consider something
like this:

<?
$q = mysql_query("select id, field from table where
status = 'new' order by id desc limit 1");
while ($r = mysql_fetch_array($q)){
print "<form action='$_SERVER[PHP_SELF]'>";
print "<input type='text' name='field' value='$r[field]' />";
print "<input type='hidden' name='id' value='$r[id]' />";
print "<input type='submit' value='Save value' />";
print "</form>";
}
?>

This will present a form to edit the last entry in the database. You could of
course edit multiple fields at the same time:

<form action='<?=$_SERVER[PHP_SELF] ?>'>
<?
$q = mysql_query("select id, field from table where
status = 'new' order by id desc");
while ($r = mysql_fetch_array($q)){
print "<input type='text' name='field[{$r[id]}]' value='$r[field]' />";
}
?>
<input type='submit' value='Save value' />
</form>

And then handle $_GET["field"] which is an array.
--
Sandman[.net]
Sep 12 '05 #5

P: n/a
rj***********@gmail.com wrote:
: I have the following.

: $result=mysql_query($sql);
: $nrows=mysql_num_rows($result);
: for ($i=0;$i<$nrows;$i++)
: {
: $row_array=mysql_fetch_row($result);
: echo "<form name='testform' action='ins_op.php' method='post'>";

: lots of form stuff

: echo "<td><input type='submit' value='Save'>";

: echo "</form>";
: }

: I'd like to have the loop wait until the form submit is pressed before
: it continues and grabs the next record from $row_array.

The problem is that you are completely misunderstanding the way this all
works.

Your script can output exactly one page. Then your script will exit.

Sometime after that the user will receive that page from the network, and
be able to see it on their browser screen.

They will look at the page and decide what to do, perhaps deciding to
press the SUBMIT button. While they are looking at the page there is _no
_program _running on the web server. Your script is not running, it
exited long before the user even starts to read the page.

When the user presses the SUBMIT button the web server will receive the
data and start up a new process to handle it. It might start the same
script as the last time, but the script itself doesn't "know" that. Your
script simply starts at the beginning each time it runs.

There are many ways and styles to handle this, but the first issue is just
understanding it.

One way to handle this is to set a hidden field in the first form that can
be used to tell what stage the user has gotten to. This is easy to
understand so I will show it. It is not alway the best way, but I think
it is easy to understand.

<?php # untested example

$where_are_we = _REQUEST['where-are-we'];
$submit_action = _REQUEST['submit'];

if ($where_are_we == '')
{
# the user has not yet visited the form at all
?>
<form action='this_script.php'>
<input type='hidden' name='where-are-we' value='1'>
<input type='submit' name='submit' value='button-one'>
<input type='submit' name='submit' value='button-two'>
</form>
<?php
}

elseif ($where_are_we == '1')
{
# The hidden field value shows that the user must be
# filling in the form created above. That form was
# created in the past. It may or may not have been
# created by this script, we have no way to know.

if ($submit_action == 'buton-one')
{
?>
<form action='this_script.php'>
<input type='hidden' name='where-are-we' value='2'>
<input type='submit' name='submit' value='confirm'>
<input type='submit' name='submit' value='ignore'>
</form>
<?php
}

else
{
?>
<form action='this_script.php'>
<input type='hidden' name='where-are-we' value='3'>
<input type='submit' name='submit' value='doit'>
<input type='submit' name='submit' value='more'>
</form>
<?php
}
}

elseif ($where_are_we == '2')
...
elseif ($where_are_we == '3')
...
...etc etc etc...
Note: personally I rarely do it quite this way. I think it is easier if
the submit button has unique values for every possible action. Then you
don't need a field like 'where_are_we', you simply check what action the
submit button has requested and try to do that action. If no action is
requested then that indicates the user has not yet seen the form, so in
that case you show the first form they need to see.

Each time the script runs it does one action. Each time it runs it has to
check that it has all the correct input to do that one action. Each time
it runs it has to check if a user is logged-in, if you are using logins.

--

This programmer available for rent.
Sep 12 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.