AJAX异步请求链接数据库

<!DOCTYPE html> 
<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);
?>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值