如何返回HTML文件作为对POST请求的响应?

时间:2010-06-23 20:08:04

标签: php

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

5 个答案:

答案 0 :(得分:16)

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

答案 1 :(得分:2)

您只需要include要返回的页面:

include( 'mypage.html' );

答案 2 :(得分:2)

很容易

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

答案 3 :(得分:1)

只需添加相关页面

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

答案 4 :(得分:1)

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

<?php

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

?>

这将起作用,但在发布数据时并不理想 - 任何页面重新加载都需要再次发布数据。这可能会导致不可思议的影响(你的行为是幂等的吗?)。

更合适的选择是使用一个PHP脚本来确定要使用的输出,然后将浏览器重定向到适当的内容。一旦用户的浏览器被重定向,页面刷新将干净地重新加载页面而不会立即产生任何不利影响。

<?php

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

?>
相关问题