我遇到了一个奇怪的问题,我无法获得由下拉触发的php网页来刷新。这是主文件(SelectForceRefresh.php):
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="chrome=1" /><!-- Optimistically rendering in Chrome Frame in IE. -->
<title>My form</title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
<script>
function ConfigFileSelection(id)
{
$.ajax({
type: "GET",
url: "RefreshConfig.php",
data: "confFile = " + id,
success: function GotHere()
{
alert("Got here!");
}
});
}
</script>
<select name="userconfigoption" onchange="ConfigFileSelection(this.value);" >
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
</body>
</html>
这里是"链接"文件(RefreshConfig.php):
<?php
if(isset($_GET['confFile'])){
CopyUserConfig($_GET['confFile']);
}
function CopyUserConfig($id)
{
header("Location: SelectForceRefresh.php");
exit;
}
?>
问题是,当我从下拉菜单中进行选择时,javascript函数会被触发,因为我看到了警报"Got here!",但其他php文件从未被触发进行刷新。我已经做了三天了,不知道我错过了什么。我在这里回顾了Stack Overflow上的其他解决方案,如下所示:
关于更改下拉列表showld调用php函数
我试图从那里学习这项技术,但它们根本不起作用。
我在这里错过了什么?两个文件都在同一目录中。提前谢谢。
您不能让AJAX函数所针对的PHP文件刷新您的网页。最好的方法是在AJAX的成功函数中做if(JavaScript也是如此)。类似于:
$.ajax({
type: "GET",
url: "RefreshConfig.php",
data: "confFile = " + id,
success: function GotHere() {
window.location.reload();
}
});