11.07.2015 Views

AJAX and PHP

AJAX and PHP

AJAX and PHP

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 4If we are dealing with classic server-side validation, we call the validate<strong>PHP</strong>() method, whichreturns the name of the page the browser should be redirected to (which will be allok.php if thevalidation was successful, or index.php if not). The validation results for each field are stored inthe session <strong>and</strong> if it gets reloaded, index.php will show the fields that didn't pass the test.In the case of <strong>AJAX</strong> calls, the server composes a response that specifies if the field is valid. Theresponse is a short XML document that looks like this:0txtUsernameIf the result is 0, then txtUsername isn't valid <strong>and</strong> should be marked accordingly. If the result is 1,the field's value is valid.Next, let's look into validate.class.php. The class constructor creates a connection to the database<strong>and</strong> the destructor closes that connection. We then have two public methods: Validate<strong>AJAX</strong> (h<strong>and</strong>les<strong>AJAX</strong> validation) <strong>and</strong> Validate<strong>PHP</strong> (h<strong>and</strong>les typical server-side validation).<strong>AJAX</strong> validation requires two parameters, one that holds the value to be validated ($inputValue)<strong>and</strong> one that holds the form field's ID ($fieldID). A switch block loads specific validation foreach form field. This function will return 0 if validation fails or 1 if validation is successful.The <strong>PHP</strong> validation function takes no parameters, as it will always validate the entire form (afterform submit). First we initialize the $errorsExist flag to 0. Whenever validation fails for a field,this flag will be set to 1 <strong>and</strong> we will know validation has failed. Then we need to make sure thatolder session variables are unset in order to ensure that older errors are cleared.We then check each form field against a set of custom-created rules. If validation fails, we raisethe flag ($errorsExist = 1) <strong>and</strong> set the session variable that sets the CSS class for error messageto error. If, in the end, the $errorsExist flag is still set to 0, it means that the whole validationhas been successful <strong>and</strong> we return the name of the success page, thus redirecting the browser tothat page.If errors are found, we save current user input into session variables, which will be used byindex.php to fill the form (remember that by default, when loading the page, all fields are empty).This is how we save current user input:foreach ($_POST as $key => $value){$_SESSION['values'][$key] = $_POST[$key];}$_POST is an array holding the names <strong>and</strong> values of all form elements, <strong>and</strong> it can be walkedthrough with foreach. This means that for each element inside the $_POST array, we create a newelement inside the $_SESSION['values'] array.There's nothing special to mention about validate.css. The success page (allok.php) is verysimple as well—it just displays a successful submission confirmation.143

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!