AJAX Form Validation - Learn step by step PART - 5 ~ Ajax Training, Learn Ajax Video Tutorials Online, Ajax Examples
Ajax Training, Learn Ajax Video Tutorials Online, Ajax Examples: AJAX Form Validation - Learn step by step PART - 5

AJAX Form Validation - Learn step by step PART - 5

9. It's time to add the business logic now. Start by creating config.php, with this code in it:

<?php

// defines database connection data

define('DB_HOST', 'localhost');

define('DB_USER', 'ajaxuser');

define('DB_PASSWORD', 'practical');

define('DB_DATABASE', 'ajax');

?>

10. Now create the error handler code in a file named error_handler.php:

<?php

// set the user error handler method to be error_handler

set_error_handler('error_handler', E_ALL);

// error handler function

function error_handler($errNo, $errStr, $errFile, $errLine)

{

// clear any output that has already been generated

if(ob_get_length()) ob_clean();

// output the error message

$error_message = 'ERRNO: ' . $errNo . chr(10) .

'TEXT: ' . $errStr . chr(10) .

'LOCATION: ' . $errFile .

', line ' . $errLine;

echo $error_message;

// prevent processing any more PHP scripts

exit;

}

?>

11. The PHP script that handles the client's AJAX calls, and also handles the validation on form submit, is validate.php:

<?php

// start PHP session

session_start();

// load error handling script and validation class

require_once ('error_handler.php');

require_once ('validate.class.php');

// Create new validator object

$validator = new Validate();

// read validation type (PHP or AJAX?)

$validationType = '';

if (isset($_GET['validationType']))

{

$validationType = $_GET['validationType'];

}

// AJAX validation or PHP validation?

if ($validationType == 'php')

{

// PHP validation is performed by the ValidatePHP method, which returns

// the page the visitor should be redirected to (which is allok.php if

// all the data is valid, or back to index.php if not)

header("Location:" . $validator->ValidatePHP());

}

else

{

// AJAX validation is performed by the ValidateAJAX method. The results

// are used to form an XML document that is sent back to the client

$response =

'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' .

'<response>' .

'<result>' .

$validator->ValidateAJAX($_POST['inputValue'], $_POST['fieldID']) .

'</result>' .

'<fieldid>' .

$_POST['fieldID'] .

'</fieldid>' .

'</response>';

// generate the response

if(ob_get_length()) ob_clean();

header('Content-Type: text/xml');

echo $response;

}

?>

Related Posts by Categories

0 comments:

Useful Links on Adobe Flex

Your Ad Here

Latest Books on Adobe Flex