Drezard Posted March 23, 2007 Share Posted March 23, 2007 Hello, I have this loop here: while (true) { if (!isset($_POST['submit'])) { echo "<MY FORM>"; } } Now, when i run this code it just displays my form (as in a html form) a couple of hundred times and doesn't stop. What I want it to do is... Run through the loop but when it gets to the echo "<MY FORM>" bit i want it to stop the loop and continue once $_POST['submit'] exists... how do I do this? So basically i want this: while (true) { if (!isset($_POST['submit'])) { echo "<MY FORM>"; // STOP THE LOOP HERE AND WAIT FOR THE USER TO SUBMIT THE FORM } } What would be the code for what i want to do? - Cheers, Daniel Link to comment https://forums.phpfreaks.com/topic/43958-pausing-a-loop/ Share on other sites More sharing options...
obsidian Posted March 23, 2007 Share Posted March 23, 2007 I think you have a small issue in your understanding of the PHP process that needs to be cleared up to help you better grasp the principles here. Remember that all PHP is going to be completely parsed out of your code before any markup is sent to the browser. Also, unlike other application programming languages like Java and C++, the code is not actively executed as the page is running. It is executed once and the page is then displayed. If you want to trap the submission of a form on the same page on which it is displayed, you can simply submit the form to the current page and check for submission before you send any output to the browser: <?php if (isset($_POST['submit'])) { // Form has been submitted, so process it } else { // Form has not been submitted, so display it } ?> Link to comment https://forums.phpfreaks.com/topic/43958-pausing-a-loop/#findComment-213414 Share on other sites More sharing options...
Drezard Posted March 23, 2007 Author Share Posted March 23, 2007 .... Okay, That wasn't my exact code.... I was just using that as an example. So theres no way to stop a loop? - Cheers, Daniel Link to comment https://forums.phpfreaks.com/topic/43958-pausing-a-loop/#findComment-213887 Share on other sites More sharing options...
per1os Posted March 23, 2007 Share Posted March 23, 2007 break; will stop it. Link to comment https://forums.phpfreaks.com/topic/43958-pausing-a-loop/#findComment-213893 Share on other sites More sharing options...
HaLo2FrEeEk Posted March 23, 2007 Share Posted March 23, 2007 break will stop it, not pause it, I got the feeling that he wanted to be able to start it back up again. Link to comment https://forums.phpfreaks.com/topic/43958-pausing-a-loop/#findComment-213902 Share on other sites More sharing options...
per1os Posted March 23, 2007 Share Posted March 23, 2007 Maybe sleep() is what he is looking for? http://us3.php.net/sleep Link to comment https://forums.phpfreaks.com/topic/43958-pausing-a-loop/#findComment-213904 Share on other sites More sharing options...
HaLo2FrEeEk Posted March 24, 2007 Share Posted March 24, 2007 Sleep will only wait a prescribed amount of time, then resume, not pause until an event takes place, php doesn't work like that, javascript does though, maybe he could look at AJAX. Link to comment https://forums.phpfreaks.com/topic/43958-pausing-a-loop/#findComment-214047 Share on other sites More sharing options...
obsidian Posted March 24, 2007 Share Posted March 24, 2007 Based on the description above, he's wanting to be inside a loop and wait for user input. That cannot happen in PHP since it is entirely server side. As I said above, you've got to receive the user input at the time the page is loading. If you want to use a form and accept the user input, you have to do that when the page reloads after submit. You could simulate the kind of thing you're after using AJAX, but I doubt it is worth your time for something like you described above. Some functions you may want to be aware of when dealing with your loops are break (mentioned above) and continue. These may help with your understanding of how the PHP loops are constructed. Good luck! Link to comment https://forums.phpfreaks.com/topic/43958-pausing-a-loop/#findComment-214169 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.