<html>
<head>
<meta charset="utf-8">
<title>AJAX</title>
<script>
function showSite(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","boss.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="users" οnchange="showSite(this.value)">
<option value="">选择一个网站:</option>
<option value="6">Google</option>
<option value="2">淘宝</option>
<option value="3">163.com</option>
<option value="4">微博</option>
<option value="5">Facebook</option>
</select>
</form>
<br>
<div id="txtHint"><b>网站信息显示在这里……</b></div>
</body>
</html>
以下是php代码
<?php
$q = isset($_GET['q'])?intval($_GET['q']):"";
if(empty($q)){
echo "请选择一个网站";
}
$con = @mysqli_connect('127.0.0.1','root','','mydb');
if(!$con){
die("数据库连接失败的原因是".mysqli_error($con));
}
mysqli_set_charset($con, 'utf8');
$arr = "select * from `websites` where id='".$q."'";
$result = mysqli_query($con, $arr);
$arra = mysqli_fetch_array($result);
echo "<table border='1'>
<tr>
<th>ID</th>
<th>网站名</th>
<th>网站 URL</th>
<th>ALEXA 排名</th>
<th>国家</th>
</tr>";
while ($arra){
echo "<tr>";
echo "<td>".$arra['id']."</td>";
echo "<td>".$arra['name']."</td>";
echo "<td>".$arra['url']."</td>";
echo "<td>".$arra['alexa']."</td>";
echo "<td>".$arra['country']."</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>