PHP处理来自一个页面的http请求,并将其显示在另一个页面上


PHP process http request from one page and display it on another

我来到这里时,对网络开发几乎没有什么了解,所以我请求大家耐心等待,因为我对这个主题一无所知。

如果你能向我展示我需要执行的逻辑步骤来完成我面前的任务,那就太好了。

在这里使用PHP。我需要弄清楚如何从一个页面/web应用程序发送GET请求并进行处理,以及在另一个页面中显示它。

一个简单的例子:

有两个php页面,一个用于发送请求send.php,另一个用于处理和显示display.php

send.php包含一个简单的表单,用于获取一些用户输入字符串,并使用get方法将其发送到display.php

<form action="display.php" method="get">
    <input type="text" name="userInput">
    <input type="submit">
</form>

display.php接收字符串并将其用作某个函数的参数,并对其进行回显。

我需要执行哪些逻辑步骤才能在一个窗口中打开send.php,在另一个窗口打开display.php,在表单中键入内容,按submit并在display.php窗口中查看结果,而不是通常从send.php重定向到display.php?据我所知,AJAX允许我们与服务器通信,但结果仍然返回到同一页面。请原谅我的无知,我寻求解决方案的逻辑步骤,也许是我目前不知道的某些技术或方法,需要研究并找到完成这项任务的方法。

实际任务是使用库存管理系统:我们需要能够用一台设备扫描条形码/id等,同时在另一个屏幕上看到有关物品的信息(想想:用安卓应用程序扫描条形码,将其发送到基于网络的库存系统,该系统在平板电脑或类似设备上打开,并在平板电脑上看到有关该物品的信息)。

注意:我们被明确告知不要使用java/scala,因为系统管理员不希望jvm与库存系统运行在同一台服务器上(似乎只有一台服务器专用于此),我认为node.js也会陷入同样的境地,尽管我不确定。

感谢您耐心处理完整的web开发noob:)

您可以使用websocket,以及像node.js一样支持它的服务器https://www.websocket.org/demos.html

通过php进行长时间轮询http://webcooker.net/ajax-polling-requests-php-jquery/

也可以使用postMessagehttps://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

因此,在更深入地分析了您的OP(原始帖子)后,我相信对您有效的方法如下:

send.php中:使用Ajax将每个扫描的条形码存储到数据库中。这样做的理由是,你不会重定向到其他地方。AJAX只会查找完成所有存储工作的php文件(您可以将其称为process.php)

display.php中:从数据库中检索所有条形码并进行渲染。这可以直接用嵌入式PHP完成,或者如果你想每隔这么多次这样做,请使用AJAX。

有了这种结构,无论您在哪个窗口或设备中,都可以看到保存的所有条形码。

让我们看看我有没有抓到你。

你想要的是,当用户点击提交时,他在同一页面打开display.php文件而不重定向,添加新信息,然后返回send.php页面?

好吧,在我看来,你可以用一些javascript和CSS来完成它。

这是我的解决方案(我的意思是,这样做)。

您需要的是一个新的<div>(或其他标签),其中包含<iframe>、一些JavaScript函数(它们在客户端运行,而不是在服务器端运行)和一些CSS。

让我们向您展示display.php文件的外观:

<form action="display.php" method="get" target="display" onSubmit="load();">
    <input type="text" name="userInput">
    <input type="submit">
</form>
<div id="mydiv" class="hide">
    <iframe name="display"></iframe>
</div>

注意,我写的表单的目标等于iframe的名称,所以它总是提交给iframe。并且,在表单中,它将在提交时进行javascript调用。此外,我还在新的div中添加了一个类,用CSS隐藏内容。CSS看起来像:

body, html{ /*to fix the page css*/
    margin: 0; 
    padding: 0;
    width: 100%;
    height: 100%;
}
#mydiv{ /*this is how it should work*/
    position: fixed;
    width: 100%;
    height: 100%;
    z-index: 1000; /*the position of the div relatively to other elements, to be always in front of them*/
    background-color: rgba(0,0,0,0.3); /*this will add an artistic background effect when loading the submit*/
}
.hide{ /*this class hide the element*/
    display: none;
}

然后你的JavaScript:

function load()
{
    var div = document.getElementById("mydiv");
    div.className = "";
}
function finish() //this will be called later, don't forget to give it parameters
{
    var div = document.getElementById("mydiv");
    div.className = "hide";
    //do what you need to show the final result here
}

现在,在你的send.php文件中,我相信你会有一个表格,所以你对它进行了类似的调用。它看起来应该是:

<form onSubmit="submitForm">
    --your form contents--
</form>

它应该有一个新的javascript函数,比如:

function submitForm()
{
    parent.finish(); //this will call the iframe's parent function, don't forget to give it parameters and something to do.
    return false; //this will stop the submit and the 'refresh page'
}

然后,你就有了自己的东西。我希望它对你有效。

我需要执行哪些逻辑步骤才能在一个窗口中打开send.php,在另一个中显示.php

试试这个:

<form action="display.php" method="get" target="_blank" >
<input type="text" name="userInput">
<input type="submit">
</form>