What is AJAX in web app? and why is it use in Web application

AJAX in web app isĀ a technique for creating fast and dynamic web pages. AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.

What is AJAX stands for?

AJAX = Asynchronous JavaScript And XML.

It is not a programming language.

It just uses a combination of:

  • A browser built-in XMLHttpRequest object (to request data from a web server)
  • JavaScript and HTML DOM (to display or use the data)
ajax in web app

AJAX in web app Example

<p>Start typing a name in the input field below:</p>
<p>Suggestions: <span id="txtHint"></span></p>

<form>
First name: <input type="text" onkeyup="showHint(this.value)">
</form>

<script>
function showHint(str) {
  if (str.length == 0) {
    document.getElementById("txtHint").innerHTML = "";
    return;
  } else {
    const xmlhttp = new XMLHttpRequest();
    xmlhttp.onload = function() {
      document.getElementById("txtHint").innerHTML = this.responseText;
    }
  xmlhttp.open("GET", "gethint.php?q=" + str);
  xmlhttp.send();
  }
}
</script>
<?php
// Array with names
$a[] = "Anna";
$a[] = "Brittany";
$a[] = "Cinderella";
$a[] = "Diana";
$a[] = "Eva";
$a[] = "Fiona";
$a[] = "Gunda";
$a[] = "Hege";
$a[] = "Inga";
$a[] = "Johanna";
$a[] = "Kitty";
$a[] = "Linda";
$a[] = "Nina";
$a[] = "Ophelia";
$a[] = "Petunia";
$a[] = "Amanda";
$a[] = "Raquel";
$a[] = "Cindy";
$a[] = "Doris";
$a[] = "Eve";
$a[] = "Evita";
$a[] = "Sunniva";
$a[] = "Tove";
$a[] = "Unni";
$a[] = "Violet";
$a[] = "Liza";
$a[] = "Elizabeth";
$a[] = "Ellen";
$a[] = "Wenche";
$a[] = "Vicky";

// get the q parameter from URL
$q = $_REQUEST["q"];

$hint = "";

// lookup all hints from array if $q is different from ""
if ($q !== "") {
  $q = strtolower($q);
  $len=strlen($q);
  foreach($a as $name) {
    if (stristr($q, substr($name, 0, $len))) {
      if ($hint === "") {
        $hint = $name;
      } else {
        $hint .= ", $name";
      }
    }
  }
}

// Output "no suggestion" if no hint was found or output correct values
echo $hint === "" ? "no suggestion" : $hint;
?>

View live demo

As you see on the html file it has a form element with input on key event. The function is called showHint() which fires an even using JavaScript. In that function you can see its getting an parameter which is called Str. Then you will see Str is a string parameter that is why it specified as Str in short form but you if want you can make as “StringSend”. But unfortunately you cant say it as String. if you try to use String that will popup an error message on your console. The reason why you can’t use it is “String” is a keyword in JavaScript. Finally in that function is sending an XMLHttp request. Whenever the back-end sends the response this function will capture it by using xmlhttp.onload function. After it capture the response it will set the value to where the element id equals to “txtHint”. In case if you have matched some names it will show you it as an array.

Finally you can see on the php back end code has raw array of names. This is not a good way to store data. But today we are leaning about AJAX. In future tutorials we can learn more about how to store data in a proper database.

References

Click here to learn about Media query in css

Leave a Reply

Your email address will not be published. Required fields are marked *