我可以在发布表单输入时重定向到新的 php 文件吗?

Can I redirect to a new php file while posting a form input?

我有一个 html 网页,我用它来获取一些用户输入。然后使用 jquery 将此输入发布到 php 脚本,该脚本又向 REST API 发送获取请求以接收 json 数据。

我的问题确实是:是否可以将我的 php 文件更改为另一个嵌入了 php 的网页,并在将变量发布到同一脚本时重定向到该网页,这样我就可以显示json 同时在新页面上生成 table。

我已经在 javascript 文件中收到了 json,我知道我可以用它来创建一个 table,我只是想知道我是否真的可以做到这一切一次进入 php 页面,因为我已经编写了使用 json 数据填充 table 的脚本。

我已经包含了我的代码的一些基本片段,以帮助解释我在做什么。

HTML:

<head>
<script type="text/javascript" src="collect_User_Input.js"></script>
</head>
<p> What is the unique id of the beacon? </p> 
<form> <input type="text" id="uniqueId" /> </form> 
<button onclick="collect_User_Input();" >Send Request</button>

JS:

var uniqueId = document.getElementById('uniqueId');
$.post("send_Request.php", { uniqueId: uniqueId.value },function(result) {
alert(result);

PHP:

$uniqueId = $_POST["uniqueId"];

(使用 curl 获取请求)

 echo ($uniqueId);

我尝试跳过 javascript 步骤并直接提交表单,但这总是给我禁止的错误消息。正如您可能已经猜到的那样,我对此很陌生,所以欢迎任何建议。

在您的 PHP 中,您很可能希望使用 json_encode return 一些 JSON: http://php.net/manual/en/function.json-encode.php

在您的 JSON 中,您可以 return 一个成功值 - 然后根据该值,您可以使用以下方式重定向:

window.location

你甚至可以有第二个属性,如果它与 uniqueID 不同,return你希望用户重定向到哪个页面:

{"success":true,"location":"/your/path/here.html"}

不利的一面是,如果出现错误,您可以 return 在您的页面上显示相关消息:

{"success":false,"message":"ID not found"}

我使用此过程在执行重定向之前检查服务器上的某些内容是否有效,这听起来与您想要执行的操作大致相同?