This article introduces how to use Ajax with PHP through an example. you can download the source program of this instance for better understanding. In the compressed package, functions. js is the core code of Ajax, and all the operations are implemented through it. The code described below is extracted from functions. j Ajax
Result 1: When you place the cursor over a notebook, if there is a memo on the day, it will be displayed, for example:
The code is as follows:
Function checkfortasks (thedate, e ){
// Find the corresponding taskbox in the page
Set 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,
The code is as follows:
Function autocomplete (thevalue, e ){
// Locate
Location
TheObject = document. getElementById ("autocompletep ");
// 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 = "autocompletep ";
// 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 );
}
Package and download files