所以我有一个像这样的JavaScript函数,
var ajax = function(data, url, method, onfinish){
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
onfinish(xmlhttp.responseText);
}
};
xmlhttp.open(method, "pages/page.php?cv=1", true);
xmlhttp.send("cv=1");
};
我有一个应该运行该函数的空链接
<a href="javascript:void(0)" onclick="ajax('cv=1', 'pages/page.php', 'POST', set)" class="link">Posts</a>
这是我的php文件,
<?php
$cv = $_POST["cv"];
if ($cv == "p1") {
include("posts.php");
} else if ($cv == "p2") {
include("users.php");
} else if ($cv == "p3") {
include("write.php");
} else if ($cv == "p4") {
include("signup.php");
}
?>
但我一直收到这个错误,
注意:第2行/home/cabox/workspace/pages/page.php中的未定义索引:cv
答案 0 :(得分:0)
你可以这样:
$cv = isset($_POST['cv']) ? $_POST['cv'] : '';
我希望可以帮到你!
答案 1 :(得分:0)
如果您要使用POST
,则需要添加:
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
所以你的功能将是:
var xmlhttp = new XMLHttpRequest();
xmlhttp.open(method, url, true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
}
}
xmlhttp.send(data);
然后在PHP中:
if(isset($_POST['cv'])) {
$cv = $_POST["cv"];
if ($cv == "1") {
include("posts.php");
} else if ($cv == "2") {
include("users.php");
} else if ($cv == "3") {
include("write.php");
} else if ($cv == "4") {
include("signup.php");
}
}