Simple AJAX example
Posted in: Programming
Once I created my first AJAX “application” (also based on some simple tutorial) I just can’t stop thinking of AJAX methodology and it’s usage all over the projects.
What is AJAX actually? By definition – Asynchronous JavaScript and XML, a technique used in web application development.
In general it is a combination of XHTML and JavaScript.
How it works? I think it’s the best to describe through a example:
- you click on a button which on it’s onClick method calls a JavaScript function (with some parameters)
- that JS function is using XMLHttpRequest (Mozilla) or Msxml2.XMLHTTP (IE) object which makes a HTTP request back to the server but without refreshing the whole web page
- that HTTP request could be for example a .PHP script which checks for certain data in MySQL database and returns (“echo”) true/false value
- at this point you’re back in JS function which does something with that returned true/false value on your HTML form or something else
Those “web 2.0” applications are full of this AJAX, everything is done without refreshing the whole web page each time a visitor makes a click. A perfect (and also a great one) example would be a Gmail.
Here’s a simple example which shows calling of XMLHttpRequest object from a XHTML page and returning the value back to the page:
XHTML page (“xhtml_page.html”)
-
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
-
-
<head>
-
<title>This is my first AJAX example</title>
-
-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-
-
<script language="javascript" type="text/javascript" src="ajax_call.js"></script>
-
<script type="text/javascript" language="javascript">
-
function ajaxGetValueFromServer(objPN) {
-
get(objPN);
-
}
-
</script>
-
</head>
-
-
<body>
-
<form name="my_form" id="my_form" method="post">
-
<p>
-
<input name="my_text_field" type="text" id="my_text_field" />
-
</p>
-
<p>
-
<input type="button" name="get value" id="get value" value="get random number from server" onclick="ajaxGetValueFromServer(this.parentNode);" />
-
</p>
-
</form>
-
</body>
-
-
</html>
-
JavaScript functions file (“ajax_call.js”)
-
-
var http_request = false;
-
-
function makePOSTRequest(url, parameters) {
-
http_request = false;
-
if (window.XMLHttpRequest) { // Mozilla, Safari, …
-
http_request = new XMLHttpRequest();
-
if (http_request.overrideMimeType) {
-
// set type accordingly to anticipated content type
-
//http_request.overrideMimeType(‘text/xml’);
-
http_request.overrideMimeType(‘text/html’);
-
}
-
}
-
else if (window.ActiveXObject) { // check if IE
-
try {
-
http_request = new ActiveXObject("Msxml2.XMLHTTP");
-
}
-
catch (e) {
-
try {
-
http_request = new ActiveXObject("Microsoft.XMLHTTP");
-
}
-
catch (e) {}
-
}
-
}
-
if (!http_request) {
-
window.alert(‘Cannot make call to a XMLHTTP module.’);
-
return false;
-
}
-
-
http_request.onreadystatechange = alertContents;
-
http_request.open(‘POST’, url, true);
-
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
-
http_request.setRequestHeader("Content-length", parameters.length);
-
http_request.setRequestHeader("Connection", "close");
-
http_request.send(parameters);
-
}
-
-
function alertContents() {
-
if (http_request.readyState == 4) {
-
if (http_request.status == 200) {
-
result = http_request.responseText;
-
document.my_form.my_text_field.value = result;
-
}
-
else {
-
window.alert(‘Error occured.’);
-
}
-
}
-
}
-
-
function get(obj) {
-
var poststr = "do_what=return_random_number";
-
makePOSTRequest(‘ajax_return_value.php’, poststr);
-
}
-
PHP processing file (“ajax_return_value.php”)
You can simply copy/paste the code above in three files (as noted), place it on a web server with PHP support and call it through your web browser, and it should work.
Just be careful with that copy/paste because the web browser renders a “different” quote signs…
Or, you can download demo files.
This is a basic of AJAX programming with a PHP on a server-side example.
Once you understand how this works, your next step would be to take a more detailed look on how to handle “multiple clicks” and maybe you’d like to check out some of those existing AJAX frameworks for your next big project…
Return to: Simple AJAX example
Social Web