Php 为什么这个AJAX函数不能正常工作?
我已经编写了一个简单的应用程序,它显示了一个职位的候选人列表,然后,在单击“招聘”按钮时,应该修改一个数据库以反映新招聘的候选人,并将其余的显示为“未招聘”。但是,该功能无法正常工作。我遇到的问题是AJAX函数似乎从未提供响应,我也不知道为什么。数据库也没有得到更新。我的档案在下面 行Php 为什么这个AJAX函数不能正常工作?,php,javascript,mysql,ajax,Php,Javascript,Mysql,Ajax,我已经编写了一个简单的应用程序,它显示了一个职位的候选人列表,然后,在单击“招聘”按钮时,应该修改一个数据库以反映新招聘的候选人,并将其余的显示为“未招聘”。但是,该功能无法正常工作。我遇到的问题是AJAX函数似乎从未提供响应,我也不知道为什么。数据库也没有得到更新。我的档案在下面 行document.getElementById(“errors”).innerHTML+=xmlhttp.readyState+“”+xmlhttp.status+“”正在更新我的html页面底部的一个div,显示
document.getElementById(“errors”).innerHTML+=xmlhttp.readyState+“”+xmlhttp.status+“
”
正在更新我的html页面底部的一个div,显示readyState
为4,而状态
为200,这意味着AJAX函数返回正确,但没有显示echo
'd响应。即使我从new_hire.php文件中删除所有代码,并简单地使文件回显“hello”代码>,则在响应文本中不返回任何内容
resumes.php:
<html>
<head>
<script type="text/javascript">
function new_hire(name){
var xmlhttp;
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}
else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
document.getElementById("errors").innerHTML+=xmlhttp.readyState+" "+xmlhttp.status+"<br>";
//this line, when removed, does not change anything. I left it in for debugging purposes.
document.getElementById("errors").innerHTML+=xmlhttp.responseText;
if (xmlhttp.readyState=4 && xmlhttp.status=200){
var others = xmlhttp.responseText.split("|");
for (i=0;i<others.length;i++){
tag = others[i].replace(" ","_");
document.getElementById(tag).innerHTML="";
}
}
}
xmlhttp.open("POST","new_hire.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("hiree="+name.replace(" ","%20")+"&position=Salespeople");
var name_tag = name.replace(" ","_");
document.getElementById(name_tag).innerHTML="(Current Employee)<br>";
}
</script>
</head>
...
</html>
职能新员工(姓名){
var-xmlhttp;
if(window.XMLHttpRequest){
xmlhttp=新的XMLHttpRequest();
}
否则{
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
document.getElementById(“错误”).innerHTML+=xmlhttp.readyState+“”+xmlhttp.status+“
”;
//这一行被删除后,不会改变任何东西。我保留它是为了调试。
document.getElementById(“errors”).innerHTML+=xmlhttp.responseText;
if(xmlhttp.readyState=4&&xmlhttp.status=200){
var others=xmlhttp.responseText.split(“|”);
对于(i=0;i请注意,if语句使用的不是比较运算符=
,而是赋值运算符=
,因此您使用的是:if(xmlhttp.readyState=4&&xmlhttp.status=200)而不是if(xmlhttp.readyState==4&&xmlhttp.status==200)
在readystate 4之前,您无法读取responseText。我理解这一点,但当我删除尝试执行此操作的行时,没有任何更改。肯定还有其他错误。如果将responseText行放入“if(xmlhttp.readystate=4&&xmlhttp.status=200){”block?请注意,您的if
语句没有使用比较运算符=
,而是使用赋值运算符=
,因此您使用的是:if(xmlhttp.readyState=4&&xmlhttp.status=200)
而不是if(xmlhttp.readyState==4&&xmlhttp.status==200)
@Yaniro。将此作为答案发布。
<?php
$hiree = $_POST['hiree'];
$pos = $_POST['position'];
$con = mysql_connect("host.name","user","pass") or die('Could not connect: ' . mysql_error());
mysql_select_db("dbname",$con);
$clear = mysql_query("UPDATE $pos SET employed=false WHERE 1=1;");
mysql_fetch_array($clear);
$reset = mysql_query("UPDATE $pos SET employed=true WHERE Name='$hiree';");
mysql_fetch_array($reset);
$people = mysql_query("SELECT Name FROM $pos WHERE employed=false;");
$array = array();
while ($row = mysql_fetch_array($people)){
array_push($array,$row['Name']);
}
mysql_close($con);
$response = join("|",$array);
echo $response;
?>