WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

ASP - AJAX and ASP


AJAX is utilized to make increasingly intelligent applications.


AJAX ASP Example

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

Example

Start composing a name in the info field below:

First name:

Suggestions:



Example Explained

In the model 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) {
    on the off chance 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 vacant (str.length == 0). In the event 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 record (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
diminish a(30)
'Top off cluster with names
a(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(10)="Johanna"
a(11)="Kitty"
a(12)="Linda"
a(13)="Nina"
a(14)="Ophelia"
a(15)="Petunia"
a(16)="Amanda"
a(17)="Raquel"
a(18)="Cindy"
a(19)="Doris"
a(20)="Eve"
a(21)="Evita"
a(22)="Sunniva"
a(23)="Tove"
a(24)="Unni"
a(25)="Violet"
a(26)="Liza"
a(27)="Elizabeth"
a(28)="Ellen"
a(29)="Wenche"
a(30)="Vicky"

'get the q parameter from URL
q=ucase(request.querystring("q"))

'query all clues from cluster if length of q>0
on the off chance that len(q)>0 then
  hint=""
  for i=1 to 30
    on the off chance that q=ucase(mid(a(i),1,len(q))) then
      on the off chance that hint="" then
        hint=a(i)
      else
        hint=hint & " , " & a(i)
      end if
    end if
  next
end if

'Yield "no suggestion" if no indication were found
'or then again yield the right values
on the off chance that hint="" then
  response.write("no suggestion")
else
  response.write(hint)
end if
%>