rgren925 Posted November 9, 2010 Share Posted November 9, 2010 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> Quote Link to comment Share on other sites More sharing options...
jdavidbakr Posted November 9, 2010 Share Posted November 9, 2010 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. Quote Link to comment Share on other sites More sharing options...
rgren925 Posted November 9, 2010 Author Share Posted November 9, 2010 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>'; } ?> Quote Link to comment Share on other sites More sharing options...
jdavidbakr Posted November 9, 2010 Share Posted November 9, 2010 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>'; Quote Link to comment Share on other sites More sharing options...
rgren925 Posted November 9, 2010 Author Share Posted November 9, 2010 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>'; ?> Quote Link to comment Share on other sites More sharing options...
jdavidbakr Posted November 9, 2010 Share Posted November 9, 2010 Whoops, my bad, you need an $i++ in the while loop Quote Link to comment Share on other sites More sharing options...
rgren925 Posted November 9, 2010 Author Share Posted November 9, 2010 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. Quote Link to comment Share on other sites More sharing options...
rgren925 Posted November 10, 2010 Author Share Posted November 10, 2010 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>'; Quote Link to comment Share on other sites More sharing options...
rgren925 Posted November 10, 2010 Author Share Posted November 10, 2010 okay. got it. $i was inside single quotes. Needs to look like this: echo '<input type="hidden" name="next_start" value='.$i.' />'; Jon - Thanks very much for getting me on the right track. Quote Link to comment Share on other sites More sharing options...
jdavidbakr Posted November 10, 2010 Share Posted November 10, 2010 You're welcome, glad you figured it out. That's the danger of typing code without running it first. Or I could say I didn't want to give you the solution without you having to figure it out Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.