Xajax and PHP: JavaScript without the pain

Automatic for the people

If the result set is empty display a message, "Catalog Id is Valid". The addAssign method sets the innerHTML of the validationMessage div.

$objResponse->addAssign("validationMessage","innerHTML","Catalog Id is Valid");

If the result has rows, set the innerHTML of the validationMessage div to "Catalog Id is not Valid".

$objResponse->addAssign("validationMessage","innerHTML","Catalog Id is not Valid");

Next, fetch values from the result set and set the values in the input form fields. Set the value attribute of the input form field elements with addAssign method. For example, the value attribute of the journal element is set as shown below.

$objResponse->addAssign("journal","value",$journal);

Also disable the submit button.

$objResponse->addAssign("submitForm","disabled",true);

Return the $objResponse object from the validateCatalogId function as an XML string.

return $objResponse->getXML();

Process the Ajax response

The XML response is sent to the xajax processor, which sends the XML response to the xajax’s JavaScript message pump. The message pump parses the XML instructions and sets the elements in the input page.

The data specified in the $xmlResponse object with addAssign; method is therefore set in the input form. The input.php script is available in resources zip.

Run the input.php script in the Apache web server with the URL http://localhost/input.php. Start adding a value for the Catalog Id field. An XMLHttpRequest request is sent to the server with each addition to the text field. The input page gets updated with response from the server that contains instructions about the validity of the Catalog Id. A message is displayed (below) to verify if the Catalog Id field value is valid.

Validating Input Field.

If a value is specified that is already defined in the Catalog table, a message "Catalog Id is not Valid" is displayed - as in our next screen shot.

Catalog Id not Valid.

A new catalog entry can be created by specifying a Catalog Id field that is valid and adding values to the other form fields, as shown in our final screen shot.

Catalog Id Valid.

Finally

Ajax has various applications such as auto completion, dynamic form validation, and partial page refreshes. In this article you have learned to use Ajax with PHP with a PHP framework for Ajax. You did not have to use an XMLHttpRequest object to open and send an Ajax request and process the Ajax response. Xajax generates the required JavaScript, thus, facilitating the development of an Ajax/PHP application. ®

Sponsored: Today’s most dangerous security threats