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

PHP開發(fā)基礎(chǔ)教程之AJAX與MySQL

AJAX 數(shù)據(jù)庫實例

AJAX可以用來和數(shù)據(jù)庫進行交互式通信

下面的例子將演示網(wǎng)頁如何通過 AJAX 從數(shù)據(jù)庫讀取信息

請在左邊的下拉列表中選擇一個客戶:

本例由四個元素組成:

  •  MySQL 數(shù)據(jù)庫

  •  簡單的 HTML 表單

  •  JavaScript

  •  PHP 頁面


數(shù)據(jù)庫

本例需要在數(shù)據(jù)庫中建立如下數(shù)據(jù)表:

70.png


HTML 表單與JavaScript

源碼見1.php

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script>
function showUser(str){
	var xmlhttp;
	//檢查是否有用戶被選擇
	if(str==""){
		document.getElementById("txt").innerHTML="";
		return;
	}
	//創(chuàng)建 XMLHttpRequest 對象
	if(window.XMLHttpRequest){
		// IE7+, Firefox, Chrome, Opera, Safari 瀏覽器執(zhí)行代碼
		xmlhttp=new XMLHttpRequest();
	}
	else{
		//IE6,IE5瀏覽器執(zhí)行代碼
		xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
	}
	//創(chuàng)建在服務(wù)器響應(yīng)就緒時執(zhí)行的函數(shù)
	xmlhttp.onreadystatechange=function(){

		if(xmlhttp.readyState==4 && xmlhttp.status==200){
			document.getElementById("txt").innerHTML=xmlhttp.responseText;
		}
	}
	//向服務(wù)器上的文件發(fā)送請求
	xmlhttp.open("GET","2.php?q="+str,true);
	xmlhttp.send();
}
</script>
</head>
<body>
<from>
	<!-- onchange 事件會在域的內(nèi)容改變時觸發(fā)
		當(dāng)用戶在上面的下拉列表中選擇某位用戶時,會執(zhí)行名為 "showUser()" 的函數(shù)
	 -->
	<select name="users" onchange="showUser(this.value)">
		<option value="">選擇一個人:</option>
		<option value="1">Peter Griffin</option>
		<option value="2">小 明</option>
		<option value="3">小 白</option>
	</select>
</from>
<br/>
<br/>
<div id="txt"><b>選擇相應(yīng)用戶,用戶信息將在這里展示出來</b></div>
</body>
</html>

源碼解釋

用戶通過下拉列表選擇后,通過onchange事件執(zhí)行showUser()函數(shù)

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

  •  檢查是否有用戶被選擇

  • 創(chuàng)建 XMLHttpRequest 對象

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

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

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


PHP 頁面

上面這段通過 JavaScript 調(diào)用的服務(wù)器頁面是名為 "2.php" 的 PHP 文件。

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

<?php
header("Content-type: text/html; charset=utf-8");
$q=$_GET["q"];
//連接數(shù)據(jù)庫
$con = mysqli_connect('localhost','root','root','test');
//判斷是否連接成功
if(!$con){
	die('連接數(shù)據(jù)庫失敗:'.mysqli_error($con));
}
//選擇數(shù)據(jù)庫
mysqli_select_db($con,"test");
//設(shè)定字符集
mysqli_query($con,'set names utf8');
//從數(shù)據(jù)庫中查出id對應(yīng)的相應(yīng)用戶信息
$sql="SELECT * FROM customer WHERE id='".$q."'";
$result=mysqli_query($con,$sql);
echo "<table border='1' cellspacing='0' cellpadding='0'>
<tr>
<th>姓</th>
<th>名</th>
<th>年齡</th>
<th>家鄉(xiāng)</th>
<th>工作</th>
</tr>
";
//循環(huán)顯示出用信息
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>";


?>

學(xué)習(xí)心得

本例子主要包括如下知識點:

  •  表單基礎(chǔ):下拉選項

  •  onchange事件:在域的內(nèi)容改變時發(fā)生

  • 函數(shù)調(diào)用、函數(shù)傳值

  • AJAX XMLHttpRequest對象的創(chuàng)建、在服務(wù)器響應(yīng)的時候執(zhí)行的函數(shù)、向服務(wù)器上的文件發(fā)送請求:見1-5講學(xué)習(xí)心得

  • HTML DOM getElementById()方法:返回對擁有指定 ID 的第一個對象的引用

  •  數(shù)據(jù)庫的創(chuàng)建、連接數(shù)據(jù)庫、選擇數(shù)據(jù)庫、設(shè)定字符集、根據(jù)id從數(shù)據(jù)庫中查詢、循環(huán)取出數(shù)據(jù)庫中的內(nèi)容

 有關(guān)數(shù)據(jù)庫的函數(shù):

  • mysqli_connect():打開一個到 MySQL 服務(wù)器的新的連接

  • mysqli_error():返回上一個 MySQL 操作產(chǎn)生的文本錯誤信息。

  • mysqli_select_db():用于更改連接的默認數(shù)據(jù)庫

  • mysqli_query():執(zhí)行某個針對數(shù)據(jù)庫的查詢

  • mysqli_fetch_array():從結(jié)果集中取得一行作為關(guān)聯(lián)數(shù)組,或數(shù)字數(shù)組,或二者兼有

Weiter lernen
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script> function showUser(str){ var xmlhttp; //檢查是否有用戶被選擇 if(str==""){ document.getElementById("txt").innerHTML=""; return; } //創(chuàng)建 XMLHttpRequest 對象 if(window.XMLHttpRequest){ // IE7+, Firefox, Chrome, Opera, Safari 瀏覽器執(zhí)行代碼 xmlhttp=new XMLHttpRequest(); } else{ //IE6,IE5瀏覽器執(zhí)行代碼 xmlhttp= new ActiveXObject("Microsoft.XMLHTTP"); } //創(chuàng)建在服務(wù)器響應(yīng)就緒時執(zhí)行的函數(shù) xmlhttp.onreadystatechange=function(){ if(xmlhttp.readyState==4 && xmlhttp.status==200){ document.getElementById("txt").innerHTML=xmlhttp.responseText; } } //向服務(wù)器上的文件發(fā)送請求 xmlhttp.open("GET","2.php?q="+str,true); xmlhttp.send(); } </script> </head> <body> <from> <!-- onchange 事件會在域的內(nèi)容改變時觸發(fā) 當(dāng)用戶在上面的下拉列表中選擇某位用戶時,會執(zhí)行名為 "showUser()" 的函數(shù) --> <select name="users" onchange="showUser(this.value)"> <option value="">選擇一個人:</option> <option value="1">Peter Griffin</option> <option value="2">小 明</option> <option value="3">小 白</option> </select> </from> <br/> <br/> <div id="txt"><b>選擇相應(yīng)用戶,用戶信息將在這里展示出來</b></div> </body> </html>
einreichenCode zurücksetzen