国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

AJAX與Mysql

實例解釋 - MySQL 數(shù)據庫

在上面的實例中,我們使用的數(shù)據庫表如下所示:

   QQ截圖20161009173957.png

實例解釋 - HTML 頁面

當用戶在上面的下拉列表中選擇某位用戶時,會執(zhí)行名為 "showUser()" 的函數(shù)。該函數(shù)由 "onchange" 事件觸發(fā):

<html>
 <head>
     <script>
         function showUser(str)
         {
             if (str=="")
             {
                 document.getElementById("txtHint").innerHTML="";
                 return;
             }
             if (window.XMLHttpRequest)
             {
                 // IE7+, Firefox, Chrome, Opera, Safari 瀏覽器執(zhí)行代碼
                 xmlhttp=new XMLHttpRequest();
             }
             else
             {
                 // IE6, IE5 瀏覽器執(zhí)行代碼
                 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>
         <option value="1">Peter Griffin</option>
         <option value="2">Lois Griffin</option>
         <option value="3">Glenn Quagmire</option>
         <option value="4">Joseph Swanson</option>
     </select>
 </form>
 <br>
 <div id="txtHint"><b>Person info will be listed here.</b></div>
 </body>
 </html>

showUser() 函數(shù)會執(zhí)行以下步驟:

·         檢查是否有用戶被選擇

·         創(chuàng)建 XMLHttpRequest 對象

·         創(chuàng)建在服務器響應就緒時執(zhí)行的函數(shù)

·         向服務器上的文件發(fā)送請求

·         請注意添加到 URL 末端的參數(shù)(q)(包含下拉列表的內容)


PHP 文件

上面這段通過 JavaScript 調用的服務器頁面是名為 "getuser.php" 的 PHP 文件。

"getuser.php" 中的源代碼會運行一次針對 MySQL 數(shù)據庫的查詢,然后在 HTML 表格中返回結果:

<?php
$q=$_GET["q"];
$con = mysqli_connect('localhost','peter','abc123','my_db');
if (!$con)
{
        die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM user WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
        echo "<tr>";
        echo "<td>" . $row['FirstName'] . "</td>";
        echo "<td>" . $row['LastName'] . "</td>";
        echo "<td>" . $row['Age'] . "</td>";
        echo "<td>" . $row['Hometown'] . "</td>";
        echo "<td>" . $row['Job'] . "</td>";
        echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>

解釋:當查詢從 JavaScript 發(fā)送到 PHP 文件時,將發(fā)生:

1.     PHP 打開一個到 MySQL 數(shù)據庫的連接

2.     找到選中的用戶

3.     創(chuàng)建 HTML 表格,填充數(shù)據,并發(fā)送回 "txtHint" 占位符

 

繼續(xù)學習
||
<html> <head> <script> function showUser(str) { if (str=="") { document.getElementById("txtHint").innerHTML=""; return; } if (window.XMLHttpRequest) { // IE7+, Firefox, Chrome, Opera, Safari 瀏覽器執(zhí)行代碼 xmlhttp=new XMLHttpRequest(); } else { // IE6, IE5 瀏覽器執(zhí)行代碼 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> <option value="1">Peter Griffin</option> <option value="2">Lois Griffin</option> <option value="3">Glenn Quagmire</option> <option value="4">Joseph Swanson</option> </select> </form> <br> <div id="txtHint"><b>Person info will be listed here.</b></div> </body> </html>
提交重置代碼
章節(jié)
筆記
提問
課件
反饋
捐贈

彈指間學會PHP編程