2011-12-30 131 views
-1

我已经编写了一个简单的应用程序,显示一份工作的候选人列表,然后,在点击一个聘请按钮时,应该改变一个数据库以反映新雇用的候选人,并将其余人员显示为未被雇用。但是,该功能无法正常工作。我遇到的问题是AJAX函数似乎从来没有提供响应,我不明白为什么。数据库也没有得到更新。我的文件在下面。为什么这个AJAX功能不能正常工作?

线document.getElementById("errors").innerHTML+=xmlhttp.readyState+" "+xmlhttp.status+"<br>";我的html页面的底部更新一个div,显示出了readyState为4,status是200,这应该意味着AJAX功能正常返回,但echo“d响应不正在显示。即使我从new_hire.php文件中删除所有代码,并且只是将文件echo "hello";删除,responseText中也不会返回任何内容。

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> 

new_hire.php(AJAX响应文件):

<?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; 

?> 
+0

您无法在readystate之前读取responseText 4. – epascarello 2011-12-30 17:57:03

+0

我明白,但是当我删除尝试这样做的行时,没有改变。一定有其他的东西是错的。 – ewok 2011-12-30 17:58:52

+0

如果将responseText行放在“if(xmlhttp.readyState = 4 && xmlhttp.status = 200){”block? – 2011-12-30 18:18:23

回答

1

请注意,您的if语句没有使用比较操作符==而是赋值运算符=因此您正在使用:if (xmlhttp.readyState=4 && xmlhttp.status=200)而不是if (xmlhttp.readyState==4 && xmlhttp.status==200)