AJAX is used to create more dynamic applications.
AJAX ASP Instances
The following example shows how a Web page communicates with the server when the user types a character in the input box:
Instance
Please type the letter (A-Z) in the following input box:
Suggestions:
Try the source code yourself
Example explanation-HTML page
The "Showhint ()" function is executed when the user types a character in the input box above. This function is triggered by the "onkeyup" event:
<! DOCTYPE html>
Source Code Explanation:If the input box is empty (str.length==0), the function empties the contents of the placeholder txthint and launches the function.
If the input box is not empty, then Showhint () performs the following steps:
- Create a XMLHttpRequest Object
- Create a function that executes when the server responds ready
- Send a request to a file on the server
- Note the parameter (q) added to the end of the URL (contains the contents of the input box)
ASP file
The server page of the above JavaScript call is an ASP file named "Gethint.asp".
The source code in "gethint.asp" checks the name array and returns the corresponding name to the browser:
<%response.expires=-1dim A (+) ' Fill up array with Namesa (1) = "Anna" A (2) = "Brittany" A (3) = "Cinderella" A (4) = "Diana" A ( 5) = "Eva" A (6) = "Fiona" A (7) = "Gunda" A (8) = "Hege" A (9) = "Inga" A (Ten) = "Johanna" A (one) = "Kitty" A (a) = "Linda" A (All) = "Nina" A ( = "Ophelia" A () = "Petunia" A (+) = "Amanda" A (+) = "Raquel" a () = "Cindy" A (+) = "Doris" a () = "Eve" A (+) = "Evita" A (22 ) = "Sunniva" A (All) = "Tove" a () = "Unni" A (+) = "Violet" A (+) = "Liza" a () = "Elizabeth" a () = "Ellen" a () = "A", "Wenche" A (30) = "Vicky" ' gets parameter qq=ucase from URL (Request.QueryString ("Q")) ' If the length q>0, then find all hints from the array if Len (q) >0 then hint= "" For I=1 to + if Q=ucase (Mid (A (i), 1,len (q))) then if hint= "and then hint=a (i) else Hint=hint &", "& A (i) End If end if NextEnd if no prompt is found, output" no suggestion "' or output the correct valuesif hint=" "then< C10/>response.write ("no suggestion") Else Response.Write (hint) end if%>
Source Code Explanation:
If JavaScript sends any text (that is, strlen ($q) is greater than 0), it occurs:
- Find names that match characters from JavaScript
- If no match is found, the response string is set to "no suggestion"
- If one or more matching names are found, the response string is set with all names
- Send the response to the placeholder "Txthint"
Http://www.w3school.com.cn/asp/asp_ajax_asp.asp
AJAX DB Instance