如何将HTML文件作为响应返回给POST请求?

12

我向一个 PHP 页面发送了一个 POST 请求,根据其内容,我希望它返回我编写的两个独立 HTML 页面中的其中一个。


请展示一些代码。你在哪里发送POST请求?是从页面中?还是在JavaScript中?还是在PHP中?使用什么函数/库? - Pekka
5个回答

20
if ($_POST['param'] == 'page1' )
    readfile('page1.html');
else
    readfile('other.html');

4
您可以直接包含您想要返回的页面:
include( 'mypage.html' );

1
PHP可以包含许多文件类型。一个被包含的文件同样可以是HTML文档——它不必被重命名。 - Jon Cram

2

这很简单

<?php
 if($_POST['somevalue'] == true){
  include 'page1.html';
 }else{
  include 'page2.html';
 }
?>

1

只需包含相关页面

 $someVar = $_POST['somevar'];
 if ($someVar == xxxxx)
    include "page1.htm";
 else
    include "page2.htm";

1

有很多直接实现的方法。您需要检查POST到PHP脚本的数据,并确定要呈现哪个HTML文档中的两个。

<?php

    if (<your logical condition here>) {
        include 'DocumentOne.html';
    } else {
        include 'DocumentTwo.html';
    }

?>

这种方法可以工作,但在POST数据时并不理想 - 任何页面重新加载都需要重新POST数据。这可能会导致不良影响(你的动作是幂等的吗?)。
更适合的选择是使用一个PHP脚本来确定要使用的输出,然后将浏览器重定向到相应的内容。一旦用户的浏览器被重定向,页面刷新将干净地重新加载页面,没有任何即时的不利影响。
<?php

    if (<your logical condition here> {
        header('Location: http://example.com/DocumentOne.html');
    } else {
        header('Location: http://example.com/DocumentTwo.html');
    }

?>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接