PHP 和 AJAX 請求

來源:互聯網
上載者:User
文章目錄
  • 例子解釋 - HTML 表單
  • 例子解釋:
AJAX 請求

在下面的 AJAX 例子中,我們將示範當使用者向 web 表單中輸入資料時,網頁如何與線上的 網頁伺服器進行通訊。

在下面的文字框中輸入名字:

Suggestions:

這個例子包括三張頁面:

  • 一個簡單的 HTML 表單
  • 一段 JavaScript
  • 一張 PHP 頁面
HTML 表單

這是 HTML 表單。它包含一個簡單的 HTML 表單和指向 JavaScript 的連結:

<html><head><script src="clienthint.js"></script> </head><body><form> First Name:<input type="text" id="txt1"onkeyup="showHint(this.value)"></form><p>Suggestions: <span id="txtHint"></span></p></body></html>
例子解釋 - HTML 表單

正如您看到的,上面的 HTML 頁面含有一個簡單的 HTML 表單,其中帶有一個名為 "txt1" 的輸入欄位。

該表單是這樣工作的:

  1. 當使用者在輸入欄位中按下並鬆開按鍵時,會觸發一個事件
  2. 當該事件被觸發時,執行名為 showHint() 的函數
  3. 表單的下面是一個名為 "txtHint" 的 <span>。它用作 showHint() 函數所返回資料的預留位置。
JavaScript

JavaScript 代碼儲存在 "clienthint.js" 檔案中,它被連結到 HTML 文檔:

var xmlHttpfunction showHint(str){if (str.length==0)  {   document.getElementById("txtHint").innerHTML=""  return  }xmlHttp=GetXmlHttpObject()if (xmlHttp==null)  {  alert ("Browser does not support HTTP Request")  return  } var url="gethint.php"url=url+"?q="+strurl=url+"&sid="+Math.random()xmlHttp.onreadystatechange=stateChanged xmlHttp.open("GET",url,true)xmlHttp.send(null)} function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {  document.getElementById("txtHint").innerHTML=xmlHttp.responseText  } }function GetXmlHttpObject(){var xmlHttp=null;try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); }catch (e) { // Internet Explorer try  {  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");  } catch (e)  {  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");  } }return xmlHttp;}
例子解釋:showHint() 函數

每當在輸入欄位中輸入一個字元,該函數就會被執行一次。

如果文字框中有內容 (str.length > 0),該函數這樣執行:

  1. 定義要發送到伺服器的 URL(檔案名稱)
  2. 把帶有輸入欄位內容的參數 (q) 添加到這個 URL
  3. 添加一個隨機數,以防伺服器使用快取檔案
  4. 調用 GetXmlHttpObject 函數來建立 XMLHTTP 對象,並在事件被觸發時告知該對象執行名為 stateChanged 的函數
  5. 用給定的 URL 來開啟開啟這個 XMLHTTP 對象
  6. 向伺服器發送 HTTP 要求

如果輸入欄位為空白,則函數簡單地清空 txtHint 預留位置的內容。

stateChanged() 函數

每當 XMLHTTP 對象的狀態發生改變,則執行該函數。

在狀態變成 4 (或 "complete")時,用響應文本填充 txtHint 預留位置 txtHint 的內容。

GetXmlHttpObject() 函數

AJAX 應用程式只能運行在完整支援 XML 的 網頁瀏覽器中。

上面的代碼調用了名為 GetXmlHttpObject() 的函數。

該函數的作用是解決為不同瀏覽器建立不同 XMLHTTP 對象的問題。

這一點在上一節中已經解釋過了。

PHP 頁面

被 JavaScript 代碼調用的伺服器頁面是一個名為 "gethint.php" 的簡單伺服器頁面。

"gethint.php" 中的代碼會檢查名字數組,然後向用戶端返回對應的名字:

<?php// Fill up array with names$a[]="Anna";$a[]="Brittany";$a[]="Cinderella";$a[]="Diana";$a[]="Eva";$a[]="Fiona";$a[]="Gunda";$a[]="Hege";$a[]="Inga";$a[]="Johanna";$a[]="Kitty";$a[]="Linda";$a[]="Nina";$a[]="Ophelia";$a[]="Petunia";$a[]="Amanda";$a[]="Raquel";$a[]="Cindy";$a[]="Doris";$a[]="Eve";$a[]="Evita";$a[]="Sunniva";$a[]="Tove";$a[]="Unni";$a[]="Violet";$a[]="Liza";$a[]="Elizabeth";$a[]="Ellen";$a[]="Wenche";$a[]="Vicky";//get the q parameter from URL$q=$_GET["q"];//lookup all hints from array if length of q>0if (strlen($q) > 0){$hint="";for($i=0; $i<count($a); $i++)  {  if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))    {    if ($hint=="")      {      $hint=$a[$i];      }    else      {      $hint=$hint." , ".$a[$i];      }    }  }}//Set output to "no suggestion" if no hint were found//or to the correct valuesif ($hint == ""){$response="no suggestion";}else{$response=$hint;}//output the responseecho $response;?>

如果存在從 JavaScript 送來的文本 (strlen($q) > 0),則:

  1. 找到與 JavaScript 所傳送的字元相匹配的名字
  2. 如果找到多個名字,把所有名字包含在 response 字串中
  3. 如果沒有找到匹配的名字,把 response 設定為 "no suggestion"
  4. 如果找到一個或多個名字,把 response 設定為這些名字
  5. 把 response 發送到 "txtHint" 預留位置
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.