This article introduces how to use Ajax and PHP by using an example to download the source code of this instance.ProgramFor better understanding. In the compressed package, functions. JS is the core of Ajax.CodeAll the operations are implemented through it. The Code explained below is extracted from functions. js.
Result 1: When you place the cursor over a notebook, if there is a memo on the day, it will be displayed, for example:
Function Checkfortasks (thedate, e ){ // Find the corresponding <div> of taskbox on the page and set it to visible Theobject = Document . Getelementbyid (" Taskbox "); Theobject. style. Visibility =" Visible "; // Initialize the taskbox location VaR Posx = 0; VaR Posy = 0; // Locate the taskbox position as the mouse position Posx = E. clientx + Document . Body. scrollleft; posy = E. clienty + Document . Body. scrolltop; theobject. style. Left = posx +" Px "; Theobject. style. Top = Posy +" Px "; // Set the PHP request page Serverpage =" Taskchecker. php? Thedate = "+ Thedate; // Set the location where PHP returns data replacement Objid =" Taskbox ";VaR OBJ = Document . Getelementbyid (objid ); // Send the request and load the returned data XMLHTTP. Open (" Get ", Serverpage); XMLHTTP. onreadystatechange = Function (){ If (XMLHTTP. readystate = 4 & XMLHTTP. Status = 200) {obj. innerhtml = XMLHTTP. responsetext ;}} XMLHTTP. Send ( Null );}
Result 2: When the mouse clicks a day to enter a name, the system will automatically retrieve whether the name exists, and you can enter the name box by selecting,
Function AutoComplete (thevalue, e ){ // Locate the <div> position of autocompletediv (displaying the tag of the retrieved name) on the page Theobject = Document . Getelementbyid (" Autocompletediv "); // Set to visible Theobject. style. Visibility =" Visible "; Theobject. style. width =" 152px "; // Set the tag location for search VaR Posx = 0; VaR Posy = 0; posx = (findposx ( Document . Getelementbyid (" Yourname ") + 1); posy = (findposy ( Document . Getelementbyid (" Yourname ") + 23); theobject. style. Left = posx +" Px "; Theobject. style. Top = Posy +" Px "; // Set the event to keyboard input VaR Theextrachar = E. Which; If (Theextrachar = undefined) {theextrachar = E. keycode ;} // Set the location for loading the search list VaR Objid =" Autocompletediv "; // Set the PHP request page and pass the name entered by the user to the previous value (taking into account the role of backspace) If (Theextrachar = 8 ){ If (Thevalue. Length = 1 ){ VaR Serverpage =" Autocomp. php ";} Else { VaR Serverpage =" Autocomp. php "+" ? Sstring = "+ Thevalue. substr (0, (thevalue.Length -1 ));}} Else { VaR Serverpage =" Autocomp. php "+" ? Sstring = "+ Thevalue + String . Fromcharcode (theextrachar );} // Send the request and load the returned data VaR OBJ = Document . Getelementbyid (objid); XMLHTTP. Open (" Get ", Serverpage); XMLHTTP. onreadystatechange =Function (){ If (XMLHTTP. readystate = 4 & XMLHTTP. Status = 200) {obj. innerhtml = XMLHTTP. responsetext ;}} XMLHTTP. Send ( Null );}
Source codeDownload