-2
我一直在嘗試在我的web應用程序中使用AJAX來加快速度。我有兩個文件,readdata.php,它包含html和ajax請求,以及data.php,它們反映了我需要異步更新的文本信息。AJAX與PHP的問題
這裏有兩個文件:
readdata.php
<!DOCTYPE html>
<html>
<head>
<title>Read Data</title>
<?php
//these two posts are required to access the database and are passed through a submit button from index.php
$rName=$_POST["registration"];
$rowId=$_POST["rowid"];
?>
<script>
function showUser(str) { //mostly copied from online ajax tutorial
if (str=="") {
document.getElementById("txtHint").innerHTML==xmlhttp.responseText;
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","data.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
}
</script>
</head>
<body>
<br>
<div id="txtHint"><b>data.php text will be listed here.</b></div>
</body>
</html>
另:
data.php
<?php
echo "Hello World"; //this is the text that I want to be displayed and asynchronously updated.
?>
</body>
</html>
當我去的index.php,然後點擊按鈕鏈接到readdata.php,只有文字「 data.php文本將在這裏列出。「被展示。任何幫助將不勝感激。
你是否在'console'中得到了任何東西? – loveNoHate 2014-09-03 17:09:07
您需要至少嘗試自己調試您的代碼。你的代碼的成功取決於一些事情。爲什麼不首先在'data.php'中'回顯'一些靜態字符串呢?再加上我已經在這裏發現了一些錯誤:''xmlhttp.send(「registration = <?php $ rName?>&rowid = <?php $ rowId?>」);' - 有一個'space'不應該有一個和'PHP'變量不會被回顯。它應該看起來像這樣:''xmlhttp.send(「registration = <?php echo $ rName?>&rowid = <?php echo $ rowId?>」);'。 – 2014-09-03 17:13:44
也不要在任何地方調用'showUser'。 – 2014-09-03 17:15:51