Jump to content

Pausing A Loop


Drezard

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.