This article is more than 1 year old
Xajax and PHP: JavaScript without the pain
Automatic for the people
Send an Ajax request
We shall create an Xajax application to validate an input field in a form. The input form takes data to add a catalog entry to the database table Catalog
. As a user begins to enter data in the input field Catalog Id, a XMLHttpRequest
is sent to the server to validate the Catalog Id value added.
If the Catalog Id is not already defined in the database, a message "Catalog Id is Valid" gets displayed. If the Catalog Id is already defined in the database, a message "Catalog Id is not Valid" gets displayed, the Create Catalog button gets disabled and field values for the Catalog Id get added to the form.
The xajax
PHP object performs the function of an intermediary between the client application and the server. In the Xajax application PHP script create a xajax
object.
$xajax = new xajax();
The server side processing is performed by PHP functions. Create PHP functions validateCatalogId($formValues)
and updateCatalog($formValues)
. Both these functions take a formValues
parameter.
function validateCatalogId($formValues){} function updateCatalog($formValues){}
Register the PHP functions with the xajax
object using the registerFunction()
method. The xajax
object creates wrapper functions for the PHP functions that may be invoked from a PHP script or an input form event handler.
$xajax->registerFunction("validateCatalogId"); $xajax->registerFunction("updateCatalog");
Xajax generates asynchronous wrapper functions for the registered PHP functions. A wrapper function name uses the format phpfunction
. Variable phpfunction
is a server-side PHP function for which a wrapper function is to be defined. Xajax provides asynchronous form processing with the getFormValues(string formId)
method. Using the getFormValues()
method, an array of form field values may be submitted as an argument to a xajax asynchronous function.
Sections of a form may also be submitted instead of the complete form with the getFormValues(string formID ,boolean submitDisabledElements, string prefix])
function. The prefix
parameter specifies that only form elements starting with that prefix should be submitted. Boolean parameter submitDisabledElements
specifies if disabled elements are to be submitted. PHP functions validateCatalogId
and updateCatalog
define a parameter for an array of form field values. Before a XMLHttpRequest
is initiated, specify the xajax
object to handle requests with the processRequests()
function.
$xajax->processRequests();