views:

205

answers:

2

The tutorial that I'm trying to figure out is this:
http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/

The username field looks like this:

<input value="" class="validate[required,custom[noSpecialCaracters],length[0,20],ajax[ajaxUser]]" type="text" name="user" id="user" />

That <input> field has a class item named ajax[ajaxUser] which has its rules contained in this script (jquery.validationEngine-en.js).

The snippet for that item looks like this:

"ajaxUser":{
    "file":"validateUser.php",
    "extraData":"name=eric",
    "alertTextOk":"* This user is available",   
    "alertTextLoad":"* Loading, please wait",
    "alertText":"* This user is already taken"},

What I can't figure out at all is how its PHP page is working which is validateUser.php. Where are all those post fields coming from? I looked around and can't find a field with those names.

Thanks

A: 

The php page (or any engine/framework you would like to use) takes the following post variables:

  • email [email protected]
  • firstname2 karnius
  • lastname2 karnius
  • name anything
  • user2 karnius

And returns either true or false as the entire response. The messages defined in your question are displayed depending on which response it gets.

Nick Craver
A: 

The post data looks like this:

validateValue=karnius&validateId=user&validateError=ajaxUser

It appears that validateId is the form element name and validateError comes from ajax[ajaxUser].

The response looks like this:

{"jsonValidateReturn":["user","ajaxUser","true"]}

The PHP code could look something like this:

<?php
    $result = mysql_query('SELECT 1 FROM users WHERE username = "'.mysql_real_escape_string($_POST['validateValue']).'"');
    $response = $result ? 'false' : 'true';
    echo json_encode(array('jsonValidateReturn' => array(
        $_POST['validateId'],
        $_POST['validateError'],
        $response)));
?>

(Obviously, a more complicated handler could base its behavior on validateId and validateError.)

bluej100