Gabriel Freitas
Gabriel Freitas

Reputation: 47

PHP runs the entire code and doesn't wait for input

I'm having a basic problem with PHP. I want to input many data by requesting from a form:

do{
        if (isset($_POST['x'])&&($_POST['y'])){
            $x = $_POST['x'];
            $y = $_POST['y'];
            //núclero de processamento
            if ($x > 0 && $y > 0){
                echo "$x    $y  primeiro quadrante";
            } elseif ($x > 0 && $y < 0){
                echo "$x    $y  segundo quadrante";
            } elseif ($x < 0 && $y < 0){
                echo "$x    $y  terceiro quadrante";
            } elseif ($x < 0 && $y > 0){
                echo "$x    $y  quarto quadrante";
            } 
        }
    } while($x && $y != 0);

In this case i have to use a loop, but here's the deal, i have to input indefinite coordinates on the form and whenever i enter a 0 it should break the loop and process the data printing out the results for each data. BUT it processes a single form, reloads the page and loops forever. What am i missing?

Upvotes: 2

Views: 84

Answers (4)

iamatstackoverflow
iamatstackoverflow

Reputation: 402

Put your if statement outside the loop

if (isset($_POST['x']) && isset($_POST['y'])){
    $x = $_POST['x'];
    $y = $_POST['y'];
    do{ 
            
        //núclero de processamento
        if ($x > 0 && $y > 0){
            echo "$x    $y  primeiro quadrante";
        } elseif ($x > 0 && $y < 0){
            echo "$x    $y  segundo quadrante";
        } elseif ($x < 0 && $y < 0){
            echo "$x    $y  terceiro quadrante";
        } elseif ($x < 0 && $y > 0){
            echo "$x    $y  quarto quadrante";
        }  
    } while($x && $y != 0);
 }

Upvotes: 1

Torbj&#246;rn Stabo
Torbj&#246;rn Stabo

Reputation: 779

The web and HTTP doesn't work that way, you can't continuously input data, wait some time and then input some more. Instead it is like htis:

  1. When you click Submit in your browser, then the browser assembles a "package" or request, and sends it to the site that the form specifies.
  2. The web server software on the receiving end receives that request. Part of the request is the path for the file/code that should process the form data that your browser sent.
  3. That code is now run/loaded by the web server. In case of a .php file the web server asks a PHP interpreter for help.
  4. The web server passes the form data that came with your request to the PHP interpreter. Now the PHP code is run, and the output(echo, var_dump, etc..) is collected by the web server.
  5. That output is then sent back to your browser from the web server.

As is hopefully shown above there is no moment of interactivity in this process. A request to a web server consists of one point in time. Otherwise you make a new/separate/different request later, to the same piece of code on the server. But the code doesn't remember anything about the last run(unless you specifically add code to save info about each request somewhere, but that's another ball game..).

The interactivity you need here would be one of

  • adding JavaScript to the webpage(checking the numbers as they're entered)
  • rewriting the PHP code so that it runs from the console(i.e locally, no web server/browser involved)(A loop, ask for one pair of XY and then output the quadrant)
  • rewrite the code so that it supports receiving indefinite number of coordinate pairs(probably two arrays in the request, one for the X coordinates and one for the Y coordinates)

Upvotes: 1

Marvin Klar
Marvin Klar

Reputation: 1917

If you want to accomplish this using a website, you need a form, where the user can input the values, then the user have to submit this form, when the data is inserted. Now you can check the input (eg. do your work with it or check, if 0 was inserted). Now you can display the resulting data and the form to input the next data. If you want to work with the data you just worked with when the user submits the next form, you either need to store these values on your server (e.g. in a database) or inside the website (e.g. using a hidden html field).

Upvotes: 0

Vidal
Vidal

Reputation: 2621

If you execute a php script is a one time shot, all the code is going to be execute, there's not stop or interaction for the user. In your case your loop never stop because you want to change the value once it was executed (this is not possible). If you want to create an interactive app on the shell with php here is the official documentation http://php.net/manual/en/features.commandline.interactive.php.

hope it helps.

Upvotes: 1

Related Questions