【发布时间】:2016-10-16 23:31:08
【问题描述】:
我已看到有关此问题的问题和答案。例如How to return a HTML file as the response to a POST request?,但在实施解决方案时遇到问题。这是一个名为 websiteIssue 的目录中的一些 php 代码示例,它不起作用,我不知道为什么。
index.php
<?php
if(isset($_POST['page']))
{
$page = $_POST['page'];
}
else
{
$page = "";
}
include 'case.php';
?>
case.php
<?php
$testLog = 'testLog.txt';
$fileHandle = fopen('testLog.txt', 'a');
fwrite($fileHandle, '$page = '.$page."\n";
switch($page)
{
case "screen2":
include 'screen2.php';
fwrite($fileHandle, 'including screen2.php'."\n");
break;
default:
include 'screen1.php';
fwrite($fileHandle, 'including screen1.php'."\n");
break;
}
fclose($fileHandle);
?>
screen1.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>screen1.php</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
</head>
<body>
<button type="button" onClick=dataSubmit({page:"screen2"})> Screen 1 => Screen2</button>
<script>
function dataSubmit(data)
{
var xmlRequest = new XMLHttpRequest();
var formData= new FormData();
for(name in data)
{
formData.append(name, data[name]);
}
xmlRequest.open('POST', 'http://localhost/websiteIssue/');
xmlRequest.send(formData);
}
</script>
</body>
</html>
screen2.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>screen2.php</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
</head>
<body>
<button type="button" onClick=dataSubmit({page:"screen1"})> Screen 2 => Screen1</button>
<script>
function dataSubmit(data)
{
var xmlRequest = new XMLHttpRequest();
var formData= new FormData();
for(name in data)
{
formData.append(name, data[name]);
}
xmlRequest.open('POST', 'http://localhost/websiteIssue/');
xmlRequest.send(formData);
}
</script>
</body>
</html>
在初始加载时,它按预期工作,screen1.php 中的 html 显示在浏览器中,但是当按下页面上的按钮时,html 保持不变,而不是更改为 screen2.php 中的html
testText.log 的输出类似于:
$page =
including screen1.php
$page = screen2
including screen2.php
很明显,我是这方面的新手,希望有一些我没有做过的基本事情。我运行它的浏览器是 Firefox。任何帮助将非常感激。
小记:我为这篇文章手动重新输入了代码,并没有运行它(运行网络服务器的机器没有连接到互联网),希望没有语法错误,但我可能在某处打错了.
【问题讨论】:
标签: javascript php html post xmlhttprequest