简体   繁体   中英

Sending data from a HTML page to a PHP script and picking the data in PHP

My HTML page

<html>
<body>
<form action="myScript.php" method="POST">
    Enter a transceiver ID:
    <br><br>
    <input type="text" name="id" >
    <br><br>
    <input type="submit" value="Submit">
</form>
</body>

My PHP script which is running on my server

<?php
$id_array = array("101");
while(1)
{
    if (isset($_POST['id']))
    { 
        $id = $_POST['id'];
        echo ("ID is ".$id."\n");
        array_push($transceiverID_array, $id);
    }
    print_r($id_array);
    usleep(5000000);
    // lot of processing will be done 
}
?>

My PHP script is in a forever while loop. If I post some data from my HTML page, my PHP script is doing something else (which takes quite a lot of time), and not necessarily processing the line where it is checking if the variable from HTML page is set. So, even if I posted something from my HTML page, due to the long duration between posting data and the PHP script going on the isset line, my PHP script is unable to pick up data.

Is there any time limit in which I need to pick up the variable in my PHP script posted from the HTML page? Is there any better way of doing this?

Also, after posting, my HTML page, keeps waiting for my PHP script to respond. Is there any way to stop loading my HTML page?

As the other commentors asked. What is the point of having an infinite loop? The server will do the task of listen for requests in your behalf!

It is natural that the HTML page doesn't receive anything, as the flow in your PHP script never ends. Remove that loop and try the request.

The problem is that you get your HTTP Parameters only once every call. If you create a infinite loop you won't get a response from the server. Even your parameter will not change because you PHP Script is called separately every time you do a request.

Why do you need to run forever? Isn't it better to do the work somewhere else eg starting a cronjob?

Your php does have a time limit: http://php.net/manual/en/function.set-time-limit.php

However it is not clear why you want this to loop forever. your while 1 loop will always state true and therefor run "infinitely" this infinity however is limited. (see link above)

EDIT: From this comment: I cannot restart my script due to the processing that it is doing. If I use a cronjob, won't my script start from the beginning every time? It will re-initialize all the variables (which I did not show here), which I do not want I cannot restart my script due to the processing that it is doing. If I use a cronjob, won't my script start from the beginning every time? It will re-initialize all the variables (which I did not show here), which I do not want

I'm not sure using PHP alone is enough to achieve what you want. You need to consider a persistence layer (memory, database) to keep your variables alive throughout multiple requests. while(1) is not how you do this.


From reading through your post, it sounds like you're misunderstanding HTTP/PHP. It looks as if you're trying to create a loop that will keep "listening" for a POST value to come through from the HTML form as if you're writing a web server itself or something.

That's not how HTTP works.

  • The client will make a GET request to get your HTML, your script then ends.
  • The client will then make a POST request with the data, your script processes it and then ends.

This happens every time, your script will run once per set of POST values that it receives. Note that your HTML isn't "waiting", your HTML was "done" in the GET request, your browser is now making a request to your PHP script and, until you have output, it will not display anything.

Now, you say it's a long running process and you don't care ie. fire-and-forget, where you're saying that you don't In this case, it may be better to simply use an AJAX request that you can fire and not care about the response. Another alternative would be to start a request into an iFrame or similar.

Either way, removing the while loop will allow you to verify that your request is going through, like so:

<?php
    $id_array = array("101");
    if (isset($_POST['id']))
    { 
        $id = $_POST['id'];
        echo ("ID is ".$id."\n");
        array_push($transceiverID_array, $id);
    }
    print_r($id_array);
?>

An option for what you're trying to do, if you want to provide feedback to the browser, is to flush content to the browser as you go.

Try POSTing to this script and see what happens:

<?php
for($i = 0; $i < 5; $i++)
{
    echo $i;
    flush(); // Send processing so far to the browser
    ob_flush(); // For good measure
    sleep(1);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM