我最近开始学习AJAX,这个小问题似乎已经出现了。我写了一个html页面,它使用这段代码创建一个AJAX连接并发送一个带有id的get请求。
function loadXML() {
var xmlhttp;
if(window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("text").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "textTesting.php?id='first'", true);
xmlhttp.send();
}
在我的textTesting.php中,为了测试,我将传入的变量$ _GET [id]与我期望为真的字符串进行比较,首先。但由于某些原因,比较似乎总是失败。
textTesting.php:
<?php
$output = "";
if(isset($_GET["id"])) {
$output = $_GET["id"];
if($output == 'first'){
$output .= " confirmed";
}
}
echo $output;
?>
我缺少PHP或AJAX的概念吗?在写这个if语句时,我期待'首次确认'成为输出。
如果需要,请提出任何进一步的问题。
答案 0 :(得分:1)
您传递的值为'first'
,但您将其与first
进行比较,因此无法匹配。
在查询字符串中,引号是数据。在PHP中,引号是字符串分隔符。
在PHP中添加引号或从查询字符串中删除它们。