Ajax Framework Tutorial Custom Module Programming

Published :
Author :
Adam Khoury
Learn to create a custom lightweight Ajax external JavaScript module, with PHP parse file example included. We can externalize code logic that may be frequently used in multiple files, which helps you avoid redundant scripting and keeps your software slimmer. If you are able, it is better to create small script modules as opposed to bloating your applications with hefty single-file Frameworks. example.html <!DOCTYPE html> <html> <head> <script src="js/ajax.js"></script> <script> // Ajax Usage Example var ajax = ajaxObj("POST", "parser.php"); ajax.onreadystatechange = function() { if(ajaxReturn(ajax) == true) { alert(ajax.responseText); } } ajax.send("name=George&country=USA"); </script> </head> <body> </body> </html> ajax.js function ajaxObj( meth, url ) { var x = new XMLHttpRequest(); x.open( meth, url, true ); x.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); return x; } function ajaxReturn(x){ if(x.readyState == 4 && x.status == 200){ return true; } } parser.php <?php if(isset($_POST["name"])){ echo $_POST["name"]." is from ".$_POST["country"]; exit(); } ?>