AJAX与PHP问题
我一直在尝试在web应用程序中使用AJAX来加快速度。我有两个文件,readdata.php,它包含html和ajax请求,data.php,它回显需要异步更新的文本信息 以下是两个文件:AJAX与PHP问题,php,ajax,Php,Ajax,我一直在尝试在web应用程序中使用AJAX来加快速度。我有两个文件,readdata.php,它包含html和ajax请求,data.php,它回显需要异步更新的文本信息 以下是两个文件: readdata.php <!DOCTYPE html> <html> <head> <title>Read Data</title> <?php //these two posts are required to acces
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>
readdata.php
读取数据
函数showUser(str){//主要是从在线ajax教程中复制的
如果(str==“”){
document.getElementById(“txtHint”).innerHTML==xmlhttp.responseText;
返回;
}
if(window.XMLHttpRequest){
//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}else{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById(“txtHint”).innerHTML=xmlhttp.responseText;
}
}
open(“POST”,“data.php”,true);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
}
这里将列出data.php文本。
另一方面:
data.php
<?php
echo "Hello World"; //this is the text that I want to be displayed and asynchronously updated.
?>
</body>
</html>
data.php
当我转到index.php并单击链接到readdata.php的按钮时,只显示文本“data.php text将在此处列出”。任何帮助都将不胜感激 好吧
我会尽量让它变得简单
我始终建议使用jQuery(如果是,请包含最新的API或将其放在主机上)
无论从read.php返回什么,都将显示在警报上。您可以通过将此脚本放入函数(){}
中来调用它,如下所示:
function syncWithAjax() {
$.ajax({
type: "POST",
url: "read.php",
data: {
name: "Brad",
surname: "Pitt"
}
})
.done(function( msg ) {
alert( "Result: " + msg );
});
}
您是否在
控制台中获得了任何信息
?您至少需要自己调试代码。代码的成功取决于很多因素。为什么不先在data.php
中添加一些静态字符串?另外,我在这里已经发现了一些错误:xmlhttp.send(“registration=&rowid=”)
-有一个空间
,其中不应该有一个空间,并且PHP
变量不会被回送。它应该是这样的:xmlhttp.send(“注册=&rowid=”)代码>。而且您不会在任何地方调用showUser
。
function syncWithAjax() {
$.ajax({
type: "POST",
url: "read.php",
data: {
name: "Brad",
surname: "Pitt"
}
})
.done(function( msg ) {
alert( "Result: " + msg );
});
}