码迷,mamicode.com
首页 > Web开发 > 详细

ajax 向php发送请求

时间:2015-06-27 18:08:07      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:

  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>  


  1. clienthint.js  
[javascript] view plaincopy
  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. }  

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. ?> 

ajax 向php发送请求

标签:

原文地址:http://www.cnblogs.com/Lance--blog/p/4604214.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!