Jump to content

Calling a form in a loop


rgren925

Recommended Posts

Hi. I'm a PHP newbie.

I'm trying to generate a form in a loop.

The user would press the submit button and the loop would iterate.

Ultimately, it's for a project that will read a large flat file and get 500 lines at a time that the user could page through.

But, my little test case is far simpler. just increment a counter every time the user presses submit.

What I've tried doesn't work; causes an endless loop rather than stopping each time for the user to hit submit.

I've just cobbled this together from things I've seen here and elsewhere, so please be gentle.

Any help would be greatly appreciated.

Thanks, Rick

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>PHP Loop Test</title>

</head>

<body>

<?php

$i = 0;

while ($i < 5) {

    echo '<form name="PHP" action="'.$PHP_SELF.'" method="POST">';

    echo '<input type="submit" name="click_php" value="PHP form" />';

    echo '</form>';

    if($_POST['click_php'])

    {

        echo "This is from PHP form ==> $i";

        $i++;

    }

}

?>

</body>

</html>

Link to comment
Share on other sites

Each time the user hits 'submit' you are creating a new connection to your webserver.  Remember that PHP is not an interactive language - the web is stateless, so you have to send a complete page.  You have to build the form so that when the user submits it then you have what you need to generate the next page.

Link to comment
Share on other sites

Thanks for the reply Jon.

Sorry to seem obtuse, but my attempt to do what i thought you were suggesting generated the same endless loop.

Is it because I'm reinitializing the counter to zero on each iteration?

If so, how do i get around that?

If not, ????

Thanks, Rick

 

<?php

$i = 0;

while ($i < 5) {

 

    echo '<html xmlns="http://www.w3.org/1999/xhtml">';

    echo '<head>';

    echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';

    echo '<title>PHP Loop Test</title>';

    echo '</head>';

    echo '<body>';

 

    echo '<form name="PHP" action="'.$PHP_SELF.'" method="POST">';

    echo '<input type="submit" name="click_php" value="PHP form" />';

    echo '</form>';

 

    if($_POST['click_php'])

    {

        echo "This is from PHP form ==> $i";

        $i++;

    }

 

    echo '</body>';

    echo '</html>';

}

?>

 

Link to comment
Share on other sites

if $_POST['click_php'] is not set when the script starts, it will never get set, and $i will never increment, which will result in an endless loop.

 

You need to set a <input type="hidden"...> element in your form, and set it to $i+5, and use that posted value to initiate $i at the beginning of your script, defaulting to 0.  Something like

 

echo '<form name="PHP" action="'.$PHP_SELF.'" method="POST" />';
if (isset($_POST['next_start'])) {
  $i = $_POST['next_start'];
} else {
  $i = 0;
}
$end = $i + 5;
while ($i < $end) {
  echo "<p>$i</p>";
}
echo '<input type="hidden" name="next_start" value="$end" />';
echo '<input type="submit" name="click_php" value="PHP form" />';
echo '</form>';

Link to comment
Share on other sites

Thanks Jon.

I think I'm getting closer to understanding this, just not certain how to fit it all together.

This generates an endless loop of zeroes -- no form:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<?php

echo '<html xmlns="http://www.w3.org/1999/xhtml">';

echo '<head>';

echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';

echo '<title>PHP Loop Test</title>';

echo '</head>';

echo '<body>';

echo '<form name="PHP" action="'.$PHP_SELF.'" method="POST" />';

if (isset($_POST['next_start'])) {

  $i = $_POST['next_start'];

}

else {

  $i = 0;

}

$end = $i + 5;

while ($i < $end) {

  echo "<p>$i</p>";

}

echo '<input type="hidden" name="next_start" value="$end" />';

echo '<input type="submit" name="click_php" value="PHP form" />';

echo '</form>';

echo '</body>';

echo '</html>';

?>

 

Link to comment
Share on other sites

Still not quite there.

The initial call generates 0 1 2 3 4 on separate lines followed by the submit button.

What I'm looking for is 0; hit submit; 1; hit submit; etc.

When I do hit submit on this I get an endless loop of

 

$end;

$ene;

$enf;

::

$enz;

$eoa;

::

etc.

Link to comment
Share on other sites

I'm thinking the $end variable isn't right.

This works except that $i isn't being saved across iterations (first iteration displays 0; subsequent display "$i")

 

echo '<form name="PHP" action="'.$PHP_SELF.'" method="POST" />';

if (isset($_POST['next_start'])) {

  $i = $_POST['next_start'];

}

else {

  $i = 0;

}

$end = 5;

if ($i < $end) {

  echo "<p>$i</p>";

  $i++;

}

echo '<input type="hidden" name="next_start" value="$i" />';

echo '<input type="submit" name="click_php" value="PHP form" />';

echo '</form>';

 

 

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.