ajax与mysql

时间:2019-01-11
本文章向大家介绍ajax与mysql,主要包括ajax与mysql使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
//index.php

<script>
        function showSite(str)
        {
            if(str=="")
            {
                document.getElementById("txtHint").innerHTML="";
                return;
            }
            if(window.XMLHttpRequest)
            {
                xmlhttp = new XMLHttpRequest();
            }
            else
            {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystate=function()
            {
                if(xmlhttp.readyState==4&&xmlhttp.status==200)
                {
                    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET","getsite_mysql.php?q="+str,true);
            xmlhttp.send();
        }
        </script>
    <body>
        <form>
            <select name="users" onchange="showSite(this.value)">
                <option value="">选择一个网站:</option>
                <option value="1">Google</option>
                <option value="2">淘宝</option>
                <option value="3">菜鸟教程</option>
                <option value="4">微博</option>
                <option value="5">Facebook</option>
            </select>
        </form>
        <div id="textHint">网站信息显示在这里.....</div>
    </body>
//getsite_mysql.php

<?php
$q = isset($_GET["q"])?intval($_GET["q"]):'';

if(empty($q)){
    echo '请选择一个网站';
    exit;
}

$con = mysqli_connect('localhost','root','');
if(!$con)
{
    echo'连接失败';
}
mysqli_select_db($con, "websites.sql");

mysqli_set_charset($con,"utf8");

$sql="select * from Websites where id = '".$q."'";

$result = mysqli_query($con, $sql);

echo "<table border='1'>'
<tr>
<th>ID</th>
<th>网站名</th>
<th>Alexa排名</th>
<th>国家</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
    echo "<tr>";
    echo "<td>".$row['id']."</td>";
    echo "<td>".$row['name']."</td>";
    echo "<td>".$row['url']."</td>";
    echo "<td>".$row['alexa']."</td>";
    echo "<td>".$row['country']."</td>";
    echo "</tr>";
}
echo "</table>";

mysqli_close($con);
?>

 

上一页 下一页

原文地址:http://www.manongjc.com/article/42948.html