简体   繁体   English

将数据从HTML页面发送到PHP脚本并在PHP中选择数据

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

My HTML page 我的HTML页面

<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脚本

<?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. 我的PHP脚本永远处于while循环中。 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. 如果我从HTML页面中发布了一些数据,则我的PHP脚本正在执行其他操作(这需要花费大量时间),并且不一定要处理检查HTML页面中的变量是否已设置的行。 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. 因此,即使我从HTML页面发布了某些内容,由于发布数据和isset行上的PHP脚本之间的持续时间很长,我的PHP脚本也无法提取数据。

Is there any time limit in which I need to pick up the variable in my PHP script posted from the HTML page? 在从HTML页面发布的PHP脚本中,是否需要提取时间限制? Is there any better way of doing this? 有什么更好的方法吗?

Also, after posting, my HTML page, keeps waiting for my PHP script to respond. 另外,发布后,我的HTML页面继续等待我的PHP脚本响应。 Is there any way to stop loading my HTML page? 有什么方法可以停止加载我的HTML页面吗?

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. HTML页面自然不接收任何内容,因为PHP脚本中的流程永远不会结束。 Remove that loop and try the request. 删除该循环,然后尝试请求。

The problem is that you get your HTTP Parameters only once every call. 问题是您每次调用仅获得一次HTTP参数。 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. 即使您的参数也不会更改,因为每次执行请求时都会分别调用PHP脚本。

Why do you need to run forever? 为什么需要永远跑步? Isn't it better to do the work somewhere else eg starting a cronjob? 在别的地方做工作更好吗,例如开始cronjob?

Your php does have a time limit: http://php.net/manual/en/function.set-time-limit.php 您的php确实有时间限制: 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. 您的while 1循环将始终为true,因此“无限”运行此无限,但是受到限制。 (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 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. 我不确定仅使用PHP是否足以实现所需的功能。 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. while(1)不是您的操作方式。


From reading through your post, it sounds like you're misunderstanding HTTP/PHP. 通过阅读您的文章,听起来您好像误解了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. 似乎您正在尝试创建一个循环,该循环将不断“侦听”来自HTML表单的POST值,就像您正在编写Web服务器本身一样。

That's not how HTTP works. 这不是HTTP的工作方式。

  • The client will make a GET request to get your HTML, your script then ends. 客户端将发出GET请求以获取您的HTML,然后脚本结束。
  • The client will then make a POST request with the data, your script processes it and then ends. 然后,客户端将使用数据发出POST请求,脚本将对其进行处理,然后结束。

This happens every time, your script will run once per set of POST values that it receives. 每次都会发生这种情况,您的脚本将针对收到的每组POST值运行一次。 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. 请注意,您的HTML不是“正在等待”,HTML是在GET请求中“完成”,您的浏览器现在正在向您的PHP脚本发出请求,直到输出为止,它不会显示任何内容。

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. 一劳永逸,你说你不这样做在这种情况下,最好使用一个可以触发而不关心响应的AJAX请求。 Another alternative would be to start a request into an iFrame or similar. 另一种选择是将请求启动到iFrame或类似框架中。

Either way, removing the while loop will allow you to verify that your request is going through, like so: 无论哪种方式,删除while循环都将使您能够验证请求正在执行中,如下所示:

<?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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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