我要为物业交易创建此网页。我使用ajax在同一页面上显示结果..用户能够提供输入,但结果没有显示......也没有错误信息.. plz help ...我' m Ajax和PHP的新手。
html代码是:
Enter City :<input class="w-input" id="city" type="text" placeholder="Enter the city name (required)" name="city" data-name="city" required="required" onchange="showUser(this.value)">
<div id="txtHint"><b>Results will be displayed Here...</b></div>
ajax代码:
<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
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("GET","buyresult.php?q="+str,true);
xmlhttp.send();
}
}
</script>
最终的buyresult.php文件内容
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="form/style.css" />
</head>
<body>
<?php
include 'connection.php';
SESSION_START();
$q = intval($_GET['q']);
$sql="SELECT * FROM property WHERE city = '".$q."'";
$qry = mysql_query($sql) or die(mysql_error());
while($result=mysql_fetch_array($qry))
{
echo '<h2 class="flat-heading">FLAT Id = '.$result['id'];
}
?>
</body>
答案 0 :(得分:1)
<script>
function showUser(str) {
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("GET","buyresult.php?q="+str,true);
xmlhttp.send();
}
</script>
我认为问题在于
document.getElementById("txtHint").innerHTML = "";
这部分..你可能没有定义任何这个id ... 而不是得到
$q = intval($_GET['q']);
使用
$q = mysql_real_escape_string($_GET['q']);
答案 1 :(得分:0)
我认为您的buyresult.php
代码应该是这样的:
您的主要问题是您正在使用$q = intval($_GET['q']);
并在查询中添加""
因此问题出在您的查询中。请转到下面一个。它应该适合你。
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="form/style.css" />
</head>
<body>
<?php
include 'connection.php';
SESSION_START();
$q = intval($_GET['q']);
$sql = "SELECT * FROM property WHERE city = $q";
$qry = mysql_query($sql) or die(mysql_error());
while($result=mysql_fetch_array($qry))
{
echo '<h2 class="flat-heading">FLAT Id = '.$result['id'];
}
?>
</body>