ジョブの候補のリストを表示し、採用ボタンをクリックすると、新しく採用された候補を反映するようにデータベースを変更し、残りを未使用として表示する単純なアプリケーションを作成しました。ただし、関数が正常に動作していません。私が抱えている問題は、AJAX関数は決して応答を出すようではないということです。理由を理解できません。データベースも更新されていません。私のファイルは以下の通りです。 このAJAX関数が正しく動作しないのはなぜですか?
ラインdocument.getElementById("errors").innerHTML+=xmlhttp.readyState+" "+xmlhttp.status+"<br>";
readyState
が4であることを示して、私のhtmlページの一番下にあるdiv要素を更新していると
status
は、AJAX機能が正常に戻ったが、
echo
「D応答がないことを意味すべきである200であり、表示されます。 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;
?>
を読んでいます。読影前にresponseTextを読むことはできません4. – epascarello
私はそれを理解しようとしている行を削除すると、変更はないことを理解しています。間違ったことがなければならない。 – ewok
"if(xmlhttp.readyState = 4 && xmlhttp.status = 200){"ブロック内にresponseText行を置くとどうなりますか? –