PHP 和 AJAX 请求

AJAX 请求

在下面的 AJAX 例子中,我们将演示当用户向 web 表单中输入数据时,网页如何与在线的 web 服务器进行通信。

HTML 表单

这是 HTML 表单。它包含一个简单的 HTML 表单和指向 JavaScript 的链接:

  1. <html>
  2. <head>
  3. <script src="clienthint.js"></script>
  4. </head>
  5.  
  6. <body>
  7.  
  8. <form>
  9. First Name:
  10. <input type="text" id="txt1"
  11. onkeyup="showHint(this.value)">
  12. </form>
  13.  
  14. <p>Suggestions: <span id="txtHint"></span></p>
  15.  
  16. </body>
  17. </html>

例子解释 - HTML 表单

正如您看到的,上面的 HTML 页面含有一个简单的 HTML 表单,其中带有一个名为 "txt1" 的输入字段。

该表单是这样工作的:

  • 当用户在输入域中按下并松开按键时,会触发一个事件
  • 当该事件被触发时,执行名为 showHint() 的函数
  • 表单的下面是一个名为 "txtHint" 的 <span>。它用作 showHint() 函数所返回数据的占位符。

JavaScript

JavaScript 代码存储在 "clienthint.js" 文件中,它被链接到 HTML 文档:

  1. var xmlHttp
  2.  
  3. function showHint(str)
  4. {
  5. if (str.length==0)
  6. {
  7. document.getElementById("txtHint").innerHTML=""
  8. return
  9. }
  10. xmlHttp=GetXmlHttpObject()
  11. if (xmlHttp==null)
  12. {
  13. alert ("Browser does not support HTTP Request")
  14. return
  15. }
  16. var url="gethint.php"
  17. url=url+"?q="+str
  18. url=url+"&sid="+Math.random()
  19. xmlHttp.onreadystatechange=stateChanged
  20. xmlHttp.open("GET",url,true)
  21. xmlHttp.send(null)
  22. }
  23.  
  24. function stateChanged()
  25. {
  26. if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  27. {
  28. document.getElementById("txtHint").innerHTML=xmlHttp.responseText
  29. }
  30. }
  31.  
  32. function GetXmlHttpObject()
  33. {
  34. var xmlHttp=null;
  35. try
  36. {
  37. // Firefox, Opera 8.0+, Safari
  38. xmlHttp=new XMLHttpRequest();
  39. }
  40. catch (e)
  41. {
  42. // Internet Explorer
  43. try
  44. {
  45. xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  46. }
  47. catch (e)
  48. {
  49. xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  50. }
  51. }
  52. return xmlHttp;
  53. }

例子解释:

showHint() 函数

每当在输入域中输入一个字符,该函数就会被执行一次。

如果文本框中有内容 (str.length > 0),该函数这样执行:

  • 定义要发送到服务器的 URL(文件名)
  • 把带有输入域内容的参数 (q) 添加到这个 URL
  • 添加一个随机数,以防服务器使用缓存文件
  • 调用 GetXmlHttpObject 函数来创建 XMLHTTP 对象,并在事件被触发时告知该对象执行名为 stateChanged 的函数
  • 用给定的 URL 来打开打开这个 XMLHTTP 对象
  • 向服务器发送 HTTP 请求

如果输入域为空,则函数简单地清空 txtHint 占位符的内容。

stateChanged() 函数

每当 XMLHTTP 对象的状态发生改变,则执行该函数。

在状态变成 4 (或 "complete")时,用响应文本填充 txtHint 占位符 txtHint 的内容。

GetXmlHttpObject() 函数

AJAX 应用程序只能运行在完整支持 XML 的 web 浏览器中。

上面的代码调用了名为 GetXmlHttpObject() 的函数。

该函数的作用是解决为不同浏览器创建不同 XMLHTTP 对象的问题。

这一点在上一节中已经解释过了。

PHP 页面

被 JavaScript 代码调用的服务器页面是一个名为 "gethint.php" 的简单服务器页面。

"gethint.php" 中的代码会检查名字数组,然后向客户端返回对应的名字:

  1. <?php
  2. // Fill up array with names
  3. $a[]="Anna";
  4. $a[]="Brittany";
  5. $a[]="Cinderella";
  6. $a[]="Diana";
  7. $a[]="Eva";
  8. $a[]="Fiona";
  9. $a[]="Gunda";
  10. $a[]="Hege";
  11. $a[]="Inga";
  12. $a[]="Johanna";
  13. $a[]="Kitty";
  14. $a[]="Linda";
  15. $a[]="Nina";
  16. $a[]="Ophelia";
  17. $a[]="Petunia";
  18. $a[]="Amanda";
  19. $a[]="Raquel";
  20. $a[]="Cindy";
  21. $a[]="Doris";
  22. $a[]="Eve";
  23. $a[]="Evita";
  24. $a[]="Sunniva";
  25. $a[]="Tove";
  26. $a[]="Unni";
  27. $a[]="Violet";
  28. $a[]="Liza";
  29. $a[]="Elizabeth";
  30. $a[]="Ellen";
  31. $a[]="Wenche";
  32. $a[]="Vicky";
  33.  
  34. //get the q parameter from URL
  35. $q=$_GET["q"];
  36.  
  37. //lookup all hints from array if length of q>0
  38. if (strlen($q) > 0)
  39. {
  40. $hint="";
  41. for($i=0; $i<count($a); $i++)
  42. {
  43. if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
  44. {
  45. if ($hint=="")
  46. {
  47. $hint=$a[$i];
  48. }
  49. else
  50. {
  51. $hint=$hint." , ".$a[$i];
  52. }
  53. }
  54. }
  55. }
  56.  
  57. //Set output to "no suggestion" if no hint were found
  58. //or to the correct values
  59. if ($hint == "")
  60. {
  61. $response="no suggestion";
  62. }
  63. else
  64. {
  65. $response=$hint;
  66. }
  67.  
  68. //output the response
  69. echo $response;
  70. ?>

如果存在从 JavaScript 送来的文本 (strlen($q) > 0),则:

  • 找到与 JavaScript 所传送的字符相匹配的名字
  • 如果找到多个名字,把所有名字包含在 response 字符串中
  • 如果没有找到匹配的名字,把 response 设置为 "no suggestion"
  • 如果找到一个或多个名字,把 response 设置为这些名字
  • 把 response 发送到 "txtHint" 占位符