黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

AJAX教程

AJAX+PHP數據庫操作

為了更清楚地說明如何使用AJAX從數據庫訪問信息,我們將動態構建MySQL查詢并在“ajax.html”上顯示結果。但在我們繼續本教程之前,需要做好一些基礎工作。使用以下命令創建表。

注:假設您有足夠的權限執行以下MySQL操作。

CREATE TABLE 'ajax_users' (
   'name' varchar(50) NOT NULL,
   'age' int(11) NOT NULL,
   'sex' varchar(1) NOT NULL,
   'wpm' int(11) NOT NULL,
   PRIMARY KEY  ('name')
)

現在使用以下SQL語句將以下數據轉儲(插入)表中:

INSERT INTO 'ajax_users' VALUES ('Jerry', 120, 'm', 20);
INSERT INTO 'ajax_users' VALUES ('Regis', 75, 'm', 44);
INSERT INTO 'ajax_users' VALUES ('Frank', 45, 'm', 87);
INSERT INTO 'ajax_users' VALUES ('Jill', 22, 'f', 72);
INSERT INTO 'ajax_users' VALUES ('Tracy', 27, 'f', 0);
INSERT INTO 'ajax_users' VALUES ('Julie', 35, 'f', 90);

客戶端HTML文件

現在我們創建客戶端HTML文件,即ajax.html,它的代碼如下:

<html>
   <body>
      <script language = "javascript" type = "text/javascript">
         <!-- 
         //Browser Support Code
         function ajaxFunction() {
            var ajaxRequest;  // The variable that makes Ajax possible!

            try {        
               // Opera 8.0+, Firefox, Safari
               ajaxRequest = new XMLHttpRequest();
            } catch (e) {

               // Internet Explorer Browsers
               try {
                  ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
               } catch (e) {

                  try {
                     ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                  } catch (e) {
                     // Something went wrong
                     alert("Your browser broke!");
                     return false;
                  }
               }
            }

            // Create a function that will receive data
            // sent from the server and will update
            // div section in the same page.
            ajaxRequest.onreadystatechange = function() {

               if(ajaxRequest.readyState == 4) {
                  var ajaxDisplay = document.getElementById('ajaxDiv');
                  ajaxDisplay.innerHTML = ajaxRequest.responseText;
               }
            }

            // Now get the value from user and pass it to
            // server script.
            var age = document.getElementById('age').value;
            var wpm = document.getElementById('wpm').value;
            var sex = document.getElementById('sex').value;
            var queryString = "?age = " + age ;

            queryString +=  "&wpm = " + wpm + "&sex=" + sex;
            ajaxRequest.open("GET", "ajax-example.php" + queryString, true);
            ajaxRequest.send(null); 
         }
         //-->
      </script>

      <form name = 'myForm'>
         Max Age: <input type = 'text' id = 'age' /> <br />
         Max WPM: <input type = 'text' id = 'wpm' /> <br />
         Sex: 

         <select id = 'sex'>
            <option value = "m">m</option>
            <option value = "f">f</option>
         </select>

         <input type = 'button' onclick = 'ajaxFunction()' value = 'Query MySQL'/>
      </form>

      <div id = 'ajaxDiv'>Your result will display here</div>
   </body>
</html>

注:在查詢中傳遞變量的方式是根據HTTP標準表單形式。

URL?variable1=value1;&variable2=value2;

上面的代碼將生成以下表單

在輸入后,結果將顯示在此部分中。

服務器端PHP文件

上面客戶端腳本已準備就緒。現在需要編寫服務器端腳本,它將從數據庫中獲取age,wpm和sex并將其發送回客戶端。將以下代碼放入文件“ajax-example.php”。

<?php
$dbhost = "localhost";
$dbuser = "dbusername";
$dbpass = "dbpassword";
$dbname = "dbname";

//Connect to MySQL Server
mysql_connect($dbhost, $dbuser, $dbpass);

//Select Database
mysql_select_db($dbname) or die(mysql_error());

// Retrieve data from Query String
$age = $_GET['age'];
$sex = $_GET['sex'];
$wpm = $_GET['wpm'];

// Escape User Input to help prevent SQL Injection
$age = mysql_real_escape_string($age);
$sex = mysql_real_escape_string($sex);
$wpm = mysql_real_escape_string($wpm);

//build query
$query = "SELECT * FROM ajax_example WHERE sex = '$sex'";

if(is_numeric($age))
   $query .= " AND age <= $age";

if(is_numeric($wpm))
   $query .= " AND wpm <= $wpm";

//Execute query
$qry_result = mysql_query($query) or die(mysql_error());

//Build Result String
$display_string = "<table>";
$display_string .= "<tr>";
$display_string .= "<th>Name</th>";
$display_string .= "<th>Age</th>";
$display_string .= "<th>Sex</th>";
$display_string .= "<th>WPM</th>";
$display_string .= "</tr>";

// Insert a new row in the table for each person returned
while($row = mysql_fetch_array($qry_result)) {
   $display_string .= "<tr>";
   $display_string .= "<td>$row[name]</td>";
   $display_string .= "<td>$row[age]</td>";
   $display_string .= "<td>$row[sex]</td>";
   $display_string .= "<td>$row[wpm]</td>";
   $display_string .= "</tr>";
}

echo "Query: " . $query . "<br />";
$display_string .= "</table>";

echo $display_string;
?>

現在嘗試在Max Age或任何其他框中輸入有效值(例如120),然后單擊Query MySQL按鈕。

如果您已成功完成本課程,那么應該撐握了如何使用MySQL,PHP,HTML和Javascript來編寫AJAX應用程序。

全部教程
主站蜘蛛池模板: 亚洲小视频在线 | 深夜在线视频免费网址 | 在线另类视频 | 精品国产欧美另类一区 | 日本操操操 | 免费观看成人毛片 | 亚洲无砖砖区免费 | 狠狠色成人综合 | 中文字幕视频在线观看 | 天天操天天干天天操 | 啪啪免费网站入口链接 | 老司机成人在线视频 | 黄色三级视频在线 | 成人理论片 | 久久亚洲精品tv | 亚洲影视天堂 | 久久99中文字幕伊人 | 欧美在线一级片 | 欧美精品第二页 | 久久国产香蕉视频 | 一区二区三区视频在线观看 | 免费色黄网站 | 夜夜拍拍 | 精品综合久久久久久8888 | 国产精品视频永久免费播放 | 天天摸夜夜添久久精品麻豆 | 亚洲一区免费 | 一级毛片视频免费 | 国产三级视频网站 | 天天干夜夜爽天天操夜夜爽视频 | 欧美人成在线观看ccc36 | 免费视频精品一区二区 | 狠狠干狠 | 中文字幕第四页 | 国产91精品不卡在线 | 99热99操99射 | 亚洲字幕 | 久青草国产97香蕉在线视频xx | 亚洲欧美日韩精品久久亚洲区色播 | 黄黄的网站在线观看 | 一区二区免费在线观看 |