Professional Documents
Culture Documents
At SJPIICD, I Matter!
LEARNING INTENT!
Terms to Ponder
Essential Content
AJAX
AJAX stands for Asynchronous JavaScript and XML, and it
allows you to fetch content from the back-end server
asynchronously, without a page refresh. Thus, it lets you update
the content of a web page without reloading it.
Let’s look at an example to understand how you could use
AJAX in your day-to-day application development. Say you want to
build a page that displays a user's profile information, with different
sections like personal information, social information, notifications,
messages, and so on.
The usual approach would be to build different web pages for
each section. So for example, users would click the social
information link to reload the browser and display a page with the
social information. This makes it slower to navigate between
sections, though, since the user has to wait for the browser to
reload and the page to render again each time.
On the other hand, you could also use AJAX to build an
interface that loads all the information without refreshing the page.
In this case, you can display different tabs for all sections, and by
clicking on the tab it fetches the corresponding content from the
back-end server and updates the page without refreshing the
browser. This helps you to improve the overall end-user experience.
AJAX is used to create more interactive applications.
Example Explained
In the example above, when a user types a character in the input
field, a function called showHint() is executed.
The function is triggered by the onkeyup event.
<html>
<body>
<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 {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET", "gethint.php?q=" + str, true);
xmlhttp.send();
}
}
</script>
</body>
</html>
Physically Detached Yet Academically Attached
Code explanation:
First, check if the input field is empty (str.length == 0). If it is,
clear the content of the txtHint placeholder and exit the function.
However, if the input field is not empty, do the following:
x Create an XMLHttpRequest object
x Create the function to be executed when the server
response is ready
x Send the request off to a PHP file (gethint.php) on the
server
x Notice that q parameter is added gethint.php?q="+str
x The str variable holds the content of the input field
<?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";
Physically Detached Yet Academically Attached
$hint = "";
SELF-SUPPORT: You can click the URL Search Indicator below to help you further understand the lessons.
Search Indicator
https://www.w3schools.com/js/js_ajax_http.asp
https://code.tutsplus.com/tutorials/how-to-use-ajax-in-php-and-jquery--cms