To save some time, and to make things easier for you I chose not to write the init.php file step by step. Because honestly it is a complete waste of time since most things are repetition of the main idea and bores the hell out of the readers. The source codes of init.php file is:

view plaincopy to clipboardprint
  1. <?php   
  2.   
  3. require ('xajax/xajax_core/xajax.inc.php');   
  4.   
  5. function checkField($fieldContents$label)   
  6. {   
  7.     $objResponse = new xajaxResponse();   
  8.      
  9.       if(emptyempty($fieldContents))   
  10.       {   
  11.           $objResponse->assign("sessionBox""innerHTML""Numbers of Errors: ".$_SESSION['formErrors']);   
  12.           $objResponse->assign($label"innerHTML"" <font color='red'>Please fill me out!</font>");   
  13.       }   
  14.       else  
  15.       {   
  16.       $objResponse->assign($label"innerHTML"" <font color='green'>Ok</font>");   
  17.     }   
  18.      
  19.     return $objResponse;   
  20. }   
  21.   
  22. function processForm($data)   
  23. {   
  24.     $objResponse = new xajaxResponse();   
  25.      
  26.       $firstName = $data['firstName'];   
  27.       $lastName = $data['lastName'];   
  28.       $emailAddress = $data['emailAddress'];   
  29.       $age = $data['age'];   
  30.       $languagesPHP = $data['languagesPHP'];   
  31.       $languagesASP = $data['languagesASP'];   
  32.       $languagesRuby = $data['languagesRuby'];   
  33.      
  34.       if(emptyempty($firstName) && emptyempty($lastName) && emptyempty($emailAddress) && emptyempty($age))   
  35.       {    
  36.           $objResponse->assign("formBox""innerHTML""<font color='red'>Please fill out the form completely.</font>");   
  37.       }   
  38.       else  
  39.       {   
  40.           //You may use the data to store them in your database or email   
  41.      
  42.           $sendEmailorDatabaseStoreWasSuccessful = true;   
  43.      
  44.           if($sendEmailorDatabaseStoreWasSuccessful)   
  45.       {   
  46.           $objResponse->assign("formBox""innerHTML""<font color='green'>The form has been successfully sent!</font>");   
  47.       }   
  48.       }   
  49.       return $objResponse;   
  50. }   
  51.   
  52. $xajax = new xajax();   
  53. //$xajax->bDebug = true;   
  54. $xajax->registerFunction("checkField");   
  55. $xajax->registerFunction("processForm");   
  56. $xajax->processRequest();   
  57. ?>  

In the above PHP code the first thing I did is included the XAJAX library. Because without it, implementing AJAX in our application will be bit hard and time consuming.

Next I've created the functions which will be called by XAJAX to check the fields and process the form. Let's explore the checkField() function in detail.

view plaincopy to clipboardprint
  1. function checkField($fieldContents$label)   
  2. {   
  3.     $objResponse = new xajaxResponse();   
  4.        
  5.     if(emptyempty($fieldContents))   
  6.     {   
  7.         $objResponse->assign("sessionBox""innerHTML""Numbers of Errors: ".$_SESSION['formErrors']);   
  8.         $objResponse->assign($label"innerHTML"" <FONT color=red>Please fill me out!</FONT>");   
  9.     }   
  10.     else  
  11.     {   
  12.         $objResponse->assign($label"innerHTML"" <FONT color=green>Ok</FONT>");   
  13.     }   
  14.        
  15.     return $objResponse;   
  16. }  

This function checks the data of a text field and depending on the content, it returns a feedback. The first parameter is the value of the field. And the second parameter is the id of the label in which the feedback will be updated. So for example when the user types his first name in the first name text field, and moves on to the last name field. AJAX will call checkField() function to check first name textfield and will passthe value of the first name field and the id of the first name label. Then checkField() will check and see if the value is empty or not. If it is empty, then the user will get a nice error saying Please fill me out, or else we can assume it is filled, and the user will get the OK message.

Next in the function you need to instantiate the xajaxResponse object in order to return a XML context. Next you tell XAJAX to completely replace the label with some HTML codes. And finally you pass in whatever your HTML code is.

I know this party is bit dodgy, just read again and you will get it :).

Finally almost at the end of the PHP file, I've created an instance of XAJAX. Told it to handle those PHP functions using AJAX and finally ordered it to process all kinds of AJAX requests :).

That's it, you are done!