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

Your name:

Reply:


Posted by Malcolm Dew-Jones on 11/29/57 11:26

rjames.clarke@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.

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация