Tuesday, September 11, 2007

Creating a MySQL connection with PHP/AJAX

In this tutorial I will explain how to open a mysql database connection using PHP and the all popular AJAX. The purpose of this article is to demonstrate how the XMLHttpRequest object works within php and the advantages of using it. This tutorial will be the first in a series of articles leading to the development of a fully functional dynamic web events application.



Asynchronous JavaScript and XML (AJAX)


AJAX is a fancy technique for creating websites that gather information from servers without having to refresh themselves. I could get into the nitty-girtty about advantages and disadvantages of using AJAX but for now all you need to know is any users action that would normally generate a HTTP request takes the form of a JavaScript call to the AJAX engine instead. The engine makes any data requests asynchronously without pausing a user's interaction with the web application. For more information on AJAX and XML click here .


Requirements


For this tutorial you should have decent knowledge of PHP and basic understanding of SQL queries. If you don't have access to a webserver you can setup your own by downloading the latest build of XAMPP . XAMPP is a easy to install Apache distribution containing MySQL, Pearl, PHP, and all sorts of useful tools and program examples.


Let's begin


Creating a request object is different depending on the users browser version. The code below checks for browsers and uses the proper request.



  1. <script language="Javascript" type="text/javascript" />


  2. function createXMLHttpRequest() {


  3. var ua;


  4. if(window.XMLHttpRequest) {

  5. try {

  6. ua = new XMLHttpRequest();

  7. } catch(e) {

  8. ua = false;

  9. }

  10. } else if(window.ActiveXObject) {

  11. try {

  12. ua = new ActiveXObject("Microsoft.XMLHTTP");

  13. } catch(e) {

  14. ua = false;

  15. }

  16. }

  17. return ua;

  18. }


  19. </script>


Next we create the AJAX request and how we want to handle the response. The sendRequest() function below will send a HTTP GET request to the server for the XML document. The handleResponse() function checks to see if the request is finished and if so returns the response.


AJAX readyState Status Codes:


0 - uninitialized

1 - loading

2 - loaded

3 - interactive

4 - complete



  1. var req = createXMLHttpRequest();


  2. function sendRequest(id) {

  3. req.open('get', 'func.php?id=' + id);

  4. req.onreadystatechange = handleResponse;

  5. req.send(null);

  6. }


  7. function handleResponse() {


  8. if(req.readyState == 4){

  9. var response = req.responseText;

  10. var update = new Array();


  11. if(response.indexOf('||' != -1)) {

  12. update = response.split('||');

  13. document.getElementById(update[0]).innerHTML = update[1];

  14. }

  15. }

  16. else


  17. alert("loading" + req.readyState);


  18. }


On to the HTML


Now we need to link to the javascript sendRequest() function.



  1. <a href="javascript:sendRequest('myRequest');">click me.</a>


  2. <span id="showDiv">


  3. </span>


Moving on..


That's all the html for now so lets move on to the php file we named func.php in the sendRequest() javascript function. This file will handle the MySQL connection.


The code within the switch statement is a simple MySQL connection done in PHP. Remember to enter your server information for $link.


That's all! Next week things will be getting more advanced and I will be posting the second tutorial.


Full code for this article:


http://www.johnwiseman.ca/articles/ajax/1/func.pht



http://www.johnwiseman.ca/articles/ajax/1/splash.html

No comments:

About Me

Ordinary People that spend much time in the box
Powered By Blogger