我使用了w3school http://www.w3schools.com/Php/php_ajax_database.asp的代码。我尝试了它,它有3种形式。但第二个不起作用。如果我在第二个下拉列表中选择一个类别,结果将为NULL。我不知道应该替换哪些代码。我是AJAX代码的初学者。任何人都可以帮助我吗?
主要表格
<?php
include('connect.php');
?>
<html>
<head>
<script>
function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
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("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="users" onchange="showUser(this.value)">
<option value=''>Select a person:</option>
<?php
$res = mysql_query("SELECT * FROM cars group by car_year order by car_year desc");
while($result = mysql_fetch_assoc($res)){
$car_year = $result["car_year"];
echo" <option value='$car_year'>$car_year</option>";
}
?>
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>
getuser.php
<html>
<head>
<script>
function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
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("GET","getuser2.php?m="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<select name='users2' onchange='showUser(this.value)'>
<option value=''></option>
<?php
$q = strval($_GET['q']);
$con = mysqli_connect('localhost','root','','matterhorn');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM cars WHERE car_year = '".$q."'";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result))
{
$car_brand = $row['car_brand'];
echo"<option value='$car_brand'>$car_brand</option>";
}
mysqli_close($con);
?>
</select>
<br><br>
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>
getuser2.php
<?php
$m = strval($_GET['m']);
$con = mysqli_connect('localhost','root','','matterhorn');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM cars WHERE car_brand = '".$m."'";
$result = mysqli_query($con,$sql);
echo"<select name='users3' onchange='showUser(this.value)'>";
echo"<option value=''></option>";
while($row = mysqli_fetch_array($result))
{
$car_brand = $row['car_brand'];
echo"<option value='$car_brand'>$car_brand</option>";
}
echo "</select>";
mysqli_close($con);
?>
答案 0 :(得分:0)
你放了javascript function in ajax file
..这不行......你创造了two javascript functions of same name
......这也是一个问题......
试试这个....
connect.php
<?php
$con = mysqli_connect('localhost', 'root', '', 'matterhorn');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con, "ajax_demo");
?>
的index.php
<?php
include('connect.php');
?>
<html>
<head>
<script>
function showUser(str)
{
if (str == "")
{
document.getElementById("txtHint").innerHTML = "";
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("GET", "getuser.php?q=" + str, true);
xmlhttp.send();
}
function showUser2(str)
{
if (str == "")
{
document.getElementById("txtHint2").innerHTML = "";
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("txtHint2").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "getuser2.php?m=" + str, true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="users" onchange="showUser(this.value)">
<option value=''>Select a person:</option>
<?php
$res = mysql_query("SELECT * FROM cars group by car_year order by car_year desc");
while ($result = mysql_fetch_assoc($res))
{
$car_year = $result["car_year"];
echo" <option value='$car_year'>$car_year</option>";
}
?>
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>
<div id="txtHint2"></div>
</body>
</html>
获取user.php
<?php include(connect.php); ?>
<select name='users2' onchange='showUser2(this.value)'>
<option value=''></option>
<?php
$q = strval($_GET['q']);
$sql = "SELECT * FROM cars WHERE car_year = '" . $q . "'";
$result = mysqli_query($con, $sql);
while ($row = mysqli_fetch_array($result))
{
$car_brand = $row['car_brand'];
echo"<option value='$car_brand'>$car_brand</option>";
}
?>
</select>
<br><br>
<div id="txtHint"><b>Person info will be listed here.</b></div>
和getuser2.php
<?php
include('connect.php');
$m = strval($_GET['m']);
$sql = "SELECT * FROM cars WHERE car_brand = '" . $m . "'";
$result = mysqli_query($con, $sql);
echo"<select name='users3'>";
echo"<option value=''></option>";
while ($row = mysqli_fetch_array($result))
{
$car_brand = $row['car_brand'];
echo"<option value='$car_brand'>$car_brand</option>";
}
echo "</select>";
?>