php 如何返回 HTML 文件作为对 POST 请求的响应?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3105124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to return a HTML file as the response to a POST request?
提问by telaviv
I send a POST request to a PHP page, and depending on what the contents are I want it to return one of two independent HTML pages I have written.
我向 PHP 页面发送 POST 请求,并根据内容是什么,我希望它返回我编写的两个独立 HTML 页面之一。
回答by Dogbert
if ($_POST['param'] == 'page1' )
readfile('page1.html');
else
readfile('other.html');
回答by Justin Ethier
回答by VOX
it's easy
这很简单
<?php
if($_POST['somevalue'] == true){
include 'page1.html';
}else{
include 'page2.html';
}
?>
回答by nico
Just include the relevant page
只需包含相关页面
$someVar = $_POST['somevar'];
if ($someVar == xxxxx)
include "page1.htm";
else
include "page2.htm";
回答by Jon Cram
There are many ways to directly implement this. You will need to examine the data POSTed to your PHP script and determine which of the two HTML documents to render.
有很多方法可以直接实现这一点。您需要检查 POST 到 PHP 脚本的数据,并确定要呈现两个 HTML 文档中的哪一个。
<?php
if (<your logical condition here>) {
include 'DocumentOne.html';
} else {
include 'DocumentTwo.html';
}
?>
This will work but is not ideal when POSTing data - any page reload will require the data to be POSTed again. This may cause underiable effects (is your action idempotent?).
这会起作用,但在发布数据时并不理想 - 任何页面重新加载都需要再次发布数据。这可能会导致不良影响(您的操作是幂等的吗?)。
A more suitable option is to use one PHP script to determine the output to use and then redirect the browser to the appropriate content. Once the user's browser has been redirected a page refresh will cleanly reload the page without any immediate adverse effects.
更合适的选择是使用一个 PHP 脚本来确定要使用的输出,然后将浏览器重定向到适当的内容。一旦用户的浏览器被重定向,页面刷新将干净地重新加载页面,而不会立即产生不利影响。
<?php
if (<your logical condition here> {
header('Location: http://example.com/DocumentOne.html');
} else {
header('Location: http://example.com/DocumentTwo.html');
}
?>

