WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

AJAX ASP Example


AJAX is utilized to make increasingly intuitive applications.


AJAX ASP Example

The following model will exhibit how a website page can speak with a web server while a client type characters in an info field:

Example

Start composing a name in the information field below:

First name:


Example Explained

In the precedent above, when a client types a character in the info field, a capacity called "showHint()" is executed.

The work is activated by the onkeyup event.

Here is the HTML code:

Example

<html>
<head>
<script>
function showHint(str) {
    in the event that (str.length == 0) {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = work() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
            }
        };
        xmlhttp.open("GET", "gethint.asp?q=" + str, true);
        xmlhttp.send();
    }
}
</script>
</head>
<body>

<p><b>Start composing a name in the input field below:</b></p>
<form>
First name: <input type="text" onkeyup="showHint(this.value)">
</form>
<p>Suggestions: <span id="txtHint"></span></p>
</body>
</html>

Code explanation:

First, check if the info field is unfilled (str.length == 0). On the off chance that it is, clear the substance of the txtHint placeholder and leave the function.

However, if the information field isn't vacant, do the following:

  • Create a XMLHttpRequest object
  • Create the capacity to be executed when the server reaction is ready
  • Send the demand off to an ASP document (gethint.asp) on the server
  • Notice that q parameter is included gethint.asp?q="+str
  • The str variable holds the substance of the information field

The ASP File - "gethint.asp"

The ASP document checks a variety of names, and returns the comparing name(s) to the browser:

<%
response.expires=-1
dim a(30)
'Fill up array with names
a(1)="Isabella"
a(2)="Ava"
a(3)="Cinderella"
a(4)="Emma"
a(5)="Eva"
a(6)="Sophia"
a(7)="Sophia"
'get the q parameter from URL
q=ucase(request.querystring("q"))

'lookup all hints from array if length of q>0
if len(q)>0 then
  hint=""
  for i=1 to 30
    if q=ucase(mid(a(i),1,len(q))) then
      if hint="" then
        hint=a(i)
      else
        hint=hint & " , " & a(i)
      end if
    end if
  next
end if

'Output "no suggestion" if no hint were found
'or output the correct values
if hint="" then
  response.write("no suggestion")
else
  response.write(hint)
end if
%>