Calling a form in a loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rgren925
Forum Newbie
Posts: 4
Joined: Tue Nov 09, 2010 2:52 pm

Calling a form in a loop

Post by rgren925 »

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

Code: Select all

<!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>
Last edited by Benjamin on Tue Nov 09, 2010 3:23 pm, edited 1 time in total.
Reason: Added [syntax=php] tags.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Calling a form in a loop

Post by Celauran »

You're getting an infinite loop because every time the form is submitted, the page reloads and, in so doing, resets $i to 0. For the value of $i to persist across reloads, you need to store it somewhere; cookies, session data, or in the URL as $_GET data.

Maybe something like this?

Code: Select all

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

    if (isset($_GET['offset'])) { $i = $_GET['offset']; }
    else { $i = 1; }

    if ($i < 6){
        echo '<form name="PHP" action="'.$PHP_SELF.'" method="get">';
        echo '<input type="hidden" name="offset" value="' . ($i + 1) . '" />';
        echo '<input type="submit" value="Next" />';
        echo '</form>';
    }
?>
    </body>
</html>
rgren925
Forum Newbie
Posts: 4
Joined: Tue Nov 09, 2010 2:52 pm

Re: Calling a form in a loop

Post by rgren925 »

Thanks very much Celauran.
that is exactly what I needed to get this going.
Rick
Post Reply