The out of box salesman

AJAX PHP Framework

AJAX PHP Framework

PHP is a scripting language and AJAX is a method of calling in asynchronous way to web-server programs or scripts.

AJAX is not any programming language. It is way of calling to web-server or script present on the web-server. AJAX calls can be made by VBSCRIPT, JAVASCRIPT as well as using popular client-side library like JQuery.  Definately, when AJAX method of calling a web-server is used, somebody e.g.script or program or application should be present on the web-server to listen to that call coming from browser and respond to it.

Take an example. When a browser requests a website say, Quora , browser sends request to web-server to give the complete HTML page for rendering. The complete HTML page consists of all the content between tags <html> and </html>.

Once the complete page is recieved by the browser, it is rendered by the browser and complete DOM (Document Object Model) of the HTML page is created.

If other web-page of the same website is needed, browser needs to make another call to web-server to get another complete page between tags <html> and </html>. The relation between browser and web-server is state-less. Neither browser or web-server records the state information of browsing and navigation from one page to another. Browser records only history of browsing which web-server can not access. Besides history, browser does not contain navigational state information of browsing.

If reloading of complete page is to be avoided, asynchronous call like ajax can be used to update only smaller portion of the web-page which needs to be changed. So ajax call can be used to update some portion of the web-page without loading complete web-page or going to entirely any other web-page. Changing of small portion of web-page means manipulating the concerned HTML element object of the complete DOM Model.

Since browser can execute JavaScripts , AJAX call can be made using JavaScripts. In following way, AJAX call can be made to web-server using JavaScript :

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script>
  5. function change_text() {
  6. var xhttp = new XMLHttpRequest();
  7. xhttp.onreadystatechange = function() {
  8. if (this.readyState == 4 && this.status == 200) {
  9. document.getElementById("text_to_be_changed"").innerHTML = this.responseText;
  10. //Change the DOM element
  11. }
  12. };
  13."GET", "new_content.txt", true);
  14. xhttp.send();
  15. }
  16. </head>
  17. </script>
  18. <body>
  19. <div id="text_to_be_changed">
  20. <h2>I am going to change after button is clicked.</h2>
  21. </div>
  22. <button type="button" onclick="change_text()">Change Text</button>
  23. </body></html>

In the above code, when button is clicked, JavaScript function "change_text()" makes ajax call to the same web-server and requests to transfer file "new_content.txt" to browser. Once the browser recieves the complete file, it changes the DOM element object having id "text_to_be_changed".
So web-page is changed without completely loading it.
The question arising here is which script or application is recieving the request ? Off course, the web-server listening on default port 80 for http requests.

Same functionality can be achieved using jQuery. jQuery is an advanced JavasSript library.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src=""
  5. integrity="sha256-a23g1Nt4dtEYOj7bR+vTu7+T8VP13humZFBJNIYoEJo="
  6. crossorigin="anonymous"></script>
  7. <script>
  8. $(document).ready(function()
  9. {
  10. var url_to_get_data = '";
  11. $.ajax({
  12. url: url_to_get_data,
  13. data: {
  14. format: 'text'
  15. },
  16. error: function() {
  17. },
  18. dataType: 'text',
  19. success: function(data) {
  20. //alert(data);
  21. document.getElementById("text_to_be_changed"").innerHTML = data;
  22. //Change the DOM element
  23. },
  24. type: 'GET'
  25. });
  26. });
  27. </head>
  28. </script>
  29. <body>
  30. <div id="text_to_be_changed">
  31. <h2>I am going to change after button is clicked.</h2>
  32. </div>
  33. <button type="button" onclick="change_text()">Change Text</button></body>
  34. </html>

In the above example, "index.html" lying in web-server root folder "public_html" is responsible for returning data to the call. If the call will be successful, "success" function will be called.
PHP script lying on web-server can be used to handle ajax call like:
  1. $(document).ready(function()
  2. {
  3. var url_to_get_data = '";
  4. //Make ajax call to my_file_controller.php on the web-server to give me my data
  5. $.ajax({
  6. url: url_to_get_data,
  7. data: {
  8. format: 'text'
  9. },
  10. error: function() {
  11. },
  12. dataType: 'text',
  13. success: function(data) {
  14. //alert(data);
  15. document.getElementById("text_to_be_changed"").innerHTML = data;
  16. //Change the DOM element
  17. },
  18. type: 'GET'
  19. });
  20. });

PHP scripts are helpful in building server logic and handling ajax requests for web-page change from front-end (UI/UX).
The above call can be handled by PHP script file “my_file_controller.php” as :
  1. <?php
  2. //my_file_controller.php
  3. echo file_get_contents('new_content.txt');
  4. //Return the content of file to the ajax caller
  5. ?>
In this way , we can combine ajax calls and php scripts to update the content on web-page without reloading.

This technique is foundation of MVC(Model-View-Controller) design pattern used for large portal and web-site development.


Post a Comment