PHP Learning 4-php and Ajax

Source: Internet
Author: User

1. Create a XMLHttpRequest Object

 1  var  xmlhttp= null  2  if  (Window. XMLHttpRequest) { 3  xmlhttp=new  XMLHttpRequest () //  4  }   5  else  if  (Window.activexobject) { 6  xmlhttp= new  ActiveXObject ("Microsoft.XMLHTTP") // // Apply Internet Expoler  7 } 

2. PHP uses AJAX instances

(1) Write an HTML form. It contains a simple HTML form and a link to JavaScript

123<script src= "Clienthint.js" ></script>45 6<body>7 8<form>9First Name:Ten<input type= "text" id= "Txt1" OneOnkeyup= "Showhint (this.value)" >//an event is triggered when the user presses and releases the key in the input field A //When the event is triggered, a function named Showhint () is executed -</form> -  the<p>suggestions: <span id= "Txthint" ></span></p> - //below the form is a <span> named "Txthint". It is used as a placeholder for the data returned by the Showhint () function.  -</body> -

(2) The JavaScript code is stored in the "clienthint.js" file, which is linked to the HTML document

1 varxmlHttp2 functionShowhint (str)//each time a character is entered in the input field, the function is executed once. 3 {4 if(str.length==0)5   { 6document.getElementById ("Txthint"). Innerhtml= ""7   return8   }9xmlhttp=Getxmlhttpobject ()Ten if(xmlhttp==NULL) One   { AAlert ("Browser does not support HTTP Request") -   return -   }  the varUrl= "gethint.php"//define the URL (file name) to send to the server -url=url+ "? q=" +str//Add the parameter (q) with the input field contents to this URL -url=url+ "&sid=" +math.random ()//add a random number to prevent the server from using cached files -Xmlhttp.onreadystatechange=statechanged//Call the Getxmlhttpobject function to create a XMLHTTP object and tell the object to execute a function named StateChanged when the event is triggered +Xmlhttp.open ("GET", URL,true)//Opens the XMLHTTP object with the given URL. -Xmlhttp.send (NULL)//sending an HTTP request to the server + }  A  at functionStateChanged ()//The function is executed whenever the state of the XMLHTTP object changes.  - {  - if(xmlhttp.readystate==4 | | xmlhttp.readystate== "COMPLETE") - //fills the contents of the Txthint placeholder Txthint with the response text when the State becomes 4 (or "complete").  -  {  -document.getElementById ("Txthint"). Innerhtml=xmlhttp.ResponseText in  }  - } to  + functionGetxmlhttpobject ()//resolves an issue that creates different XMLHTTP objects for different browsers.  - { the varxmlhttp=NULL; * Try $  {Panax Notoginseng  //Firefox, Opera 8.0+, Safari -xmlhttp=NewXMLHttpRequest (); the  } + Catch(e) A  { the  //Internet Explorer +  Try -   { $xmlhttp=NewActiveXObject ("Msxml2.xmlhttp"); $   } -  Catch(e) -   { thexmlhttp=NewActiveXObject ("Microsoft.XMLHTTP"); -   }Wuyi  } the returnxmlHttp; -}

(3) The server page called by JavaScript code is a simple server page called "gethint.php".

The code in "gethint.php" checks the name array and returns the corresponding name to the client:

1<?PHP2 //Fill up array with names3 $a[]= "Anna";4 $a[]= "Brittany";5 $a[]= "Cinderella";6 $a[]= "Diana";7 $a[]= "Eva";8 $a[]= "Fiona";9 $a[]= "Gunda";Ten $a[]= "Hege"; One $a[]= "Inga"; A $a[]= "Johanna"; - $a[]= "Kitty"; - $a[]= "Linda"; the $a[]= "Nina"; - $a[]= "Ophelia"; - $a[]= "Petunia"; - $a[]= "Amanda"; + $a[]= "Raquel"; - $a[]= "Cindy"; + $a[]= "Doris"; A $a[]= "Eve"; at $a[]= "Evita"; - $a[]= "Sunniva"; - $a[]= "Tove"; - $a[]= "Unni"; - $a[]= "Violet"; - $a[]= "Liza"; in $a[]= "Elizabeth"; - $a[]= "Ellen"; to $a[]= "Wenche"; + $a[]= "Vicky"; -  the //get the Q parameter from URL * $q=$_get["Q"]; $ Panax Notoginseng //Lookup all hints from array if length of q>0 - if(strlen($q) > 0) the { + $hint=""; A  for($i= 0;$i<Count($a);$i++) the   { +   if(Strtolower($q)==Strtolower(substr($a[$i],0,strlen($q)))) -     { $     if($hint=="") $       { -       $hint=$a[$i]; -       } the     Else -       {Wuyi       $hint=$hint." , ".$a[$i]; the       } -     } Wu   } - } About  $ //Set output to "no suggestion" if no hint were found - //or to the correct values - if($hint== "") - { A $response= "No Suggestion"; + } the Else - { $ $response=$hint; the } the  the //Output The response the Echo $response; -?>

(* organized from W3school)

PHP Learning 4-php and Ajax

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.