標籤:xhtml 1.0 oct order sel 響應 div nec orm logs
ajax(Asynchronous Javascript and XML)非同步javascrip和XMl.
ajax只是一種web互動方法。在用戶端(瀏覽器)和服務區段之間傳輸少量的資訊。從而為使用者提供及時的響應。
在傳統的web應用程式中,瀏覽器本身負責初始化到服務區的請求,並處理來自伺服器的響應。而ajax模式不同,他提供了·一個中介層裡處理這種通訊。ajax引擎實際上只是一個javascript對象或函數,只有當需要從伺服器擷取資訊時才調用它。
伺服器方面配置向ajax引擎返回其可用的資料,這些資料可以是純文字,XML或者需要的任何格式。唯一的要求就是ajax可以處理這些資料。
隨著html幀的引入 早期的ajax雛形出現但是沒有用到ajax對象,那就是引用幀技術了。
基本就是把串連給其他的幀,不用去開啟另外的一個頁面。這裡最適合的就是伺服器與用戶端之間的通訊(其實一開始對ajax的理解就是不重新整理頁面就可以局部的改變頁面。例如地圖)
這裡講引用幀的介紹了 直接貼代碼了。
第一個html頁面就叫a.html吧
<frameset rows="100%,0" style="border:0px"><frame name="displayFrame" src="DataEntry.php" noresize="noresize"></frame><frame name="hiddenFrame" src="about:blank" noresize="noresize"></frame></frameset><script type="text/javascript">function requestCustomerInfo(){var sId=document.getElementById("txtCustomerId").value;top.frames["hiddenFrame"].location="getcustomerdata.php?id="+sId;}function displayCustomerInfo(sText){var divCustomerInfo=document.getElementById("divCustomerInfo");divCustomerInfo.innerHTML=sText;}</script>
用幀來顯示兩個頁面一個提供表格顯示,另外一個串連伺服器。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head> <title>Customer Account Information</title> <script type="text/javascript">//<![CDATA[ function requestCustomerInfo() { var sId = document.getElementById("txtCustomerId").value; top.frames["hiddenFrame"].location = "GetCustomerData.php?id=" + sId; } function displayCustomerInfo(sText) { var divCustomerInfo = document.getElementById("divCustomerInfo"); divCustomerInfo.innerHTML = sText; }//]]> </script></head><body> <p>Enter customer ID number to retrieve information:</p> <p>Customer ID: <input type="text" id="txtCustomerId" value="" /></p> <p><input type="button" value="Get Customer Info" onclick="requestCustomerInfo()" /></p> <div id="divCustomerInfo"></div></body></html>
這裡的代碼 top.frames["hiddenFrame"].location="getcustomerdata.php?id="+sId為隱藏幀指派url.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Get Customer Data</title>
<?php
//customer ID
$sID = $_GET["id"];//擷取input值
//validation for ID
if (is_numeric($sID) { //判斷是不是number
//variable to hold customer info
$sInfo = "";
//database information
$sDBServer = "your.database.server";
$sDBName = "your_db_name";
$sDBUsername = "your_db_username";
$sDBPassword = "your_db_password";
//create the SQL query string
$sQuery = "Select * from Customers where CustomerId=".$sID;
//make the database connection
$oLink = mysql_connect($sDBServer,$sDBUsername,$sDBPassword);
@mysql_select_db($sDBName) or $sInfo = "Unable to open database";
if ($sInfo == ""){
if($oResult = mysql_query($sQuery) and mysql_num_rows($oResult) > 0) {
$aValues = mysql_fetch_array($oResult,MYSQL_ASSOC);
$sInfo = $aValues[‘Name‘]."<br />".$aValues[‘Address‘]."<br />".
$aValues[‘City‘]."<br />".$aValues[‘State‘]."<br />".
$aValues[‘Zip‘]."<br /><br />Phone: ".$aValues[‘Phone‘]."<br />".
"<a href=\"mailto:".$aValues[‘Email‘]."\">".$aValues[‘Email‘]."</a>";
mysql_free_result($oResult);
} else {
$sInfo = "Customer with ID $sID doesn‘t exist.";
}
}
mysql_close($oLink);
} else {
$sInfo = "Invalid customer ID.";
}
?>
<script type="text/javascript">
//<![CDATA[
window.onload = function () {
var divInfoToReturn = document.getElementById("divInfoToReturn");//擷取id=id="divInfoToReturn"的值作為參數傳遞給top.frames["displayFrame"]的displayCustomerInfo函數。
top.frames["displayFrame"].displayCustomerInfo(divInfoToReturn.innerHTML);
};
//]]>
</script>
</head>
<body>
<div id="divInfoToReturn"><?php echo $sInfo ?></div>
</body>
</html>
ajax的發展