I have been struggling with the validation plugin for a while. And I finally got it to work. As a result I created a multi-purpose lookup script. I hope this comes in handy for someone else. This script can be used for the validation plugin or for returning field values back to your jquery event. The script is quite short and I provided some documentation and examles below the code. It is only meant to find 1 record, but you can enhance for your needs.
LOOKUP.PHP <? include("config.php"); $table = $_POST["table"]; $key = $_POST["key"]; $value = $_POST["value"]; $failmsg = (isset($_POST["failmsg"]) ? $_POST["failmsg"] : $value.": Not Found in TABLE: ".$table." for KEY: ".$key); $successmsg = (isset($_POST["successmsg"]) ? $_POST["successmsg"] : $value.": Found in TABLE: ".$table." for KEY: ".$key); $fields = (isset($_POST["return_fields"]) ? $_POST["return_fields"] : "*"); $sql="SELECT $fields FROM $table WHERE $key='$value'" ; $result=mysql_query($sql); if (mysql_num_rows($result) > 0) { if ($fields == "*") { print_r($successmsg); } else { $successmsg = mysql_fetch_assoc($result); print_r("data=".json_encode($successmsg)); } } else {echo $failmsg; }; ?> <? /* This lookup file is a multi-purpose lookup function. It is meant to work with a POST transaction. This file makes the assumption that you already have a DB CONNECTION. I make my connection in CONFIG.PHP There are 3 mandatory parameters that need to be set prior to the POST. TABLE is mandatory. We need to know which table to perform the lookup on. KEY is mandatory. We need to know which fieldname we will be searching on. VALUE is mandatory. We need to know what we are looking or in the KEY field. FAILMSG is optional. Unless you are using this function for validation. see VALIDATION below. SUCCESSMSG is optional. Unless you are using this function for validation. see VALIDATION below. FIELDS is optional. If you set this to anything but "*" it will override the SUCCESSMSG and return the results as a JSON encoded string. If you send comma delimited string of field names. There are 3 options for this paramter. 1. Dont send it at all. 2. Send it as "*", which is same as not sending it. 3. Send a comma delimited string of valid field names. USING IT FOR VALIDATION When using it to check for duplicate values with jquery validation plugin... You want to have the incoming msg parameters set as (failmsg = "true" , successmsg = "false") Its a little reverse logic because when you are checking for duplicates you really dont want to find anything. EXAMPLE: USERNAME check for duplicates. useful for registration form. var validator = $("#myform").validate({ rules: { username: {required: true, minlength: 4, remote: {url:"lookup.php", type : "post", data: {table:"members", failmsg:"true", successmsg:"false", key:"username", value:function(){return $("#username").val (); } } } }, messages: { username: {required: "Enter a username", minlength: jQuery.format("Enter at least {0} characters"), remote : "it already exists" } }, [MORE VALIDATION RULES GO HERE] }); USING IT FOR RETURNING DATA You can also use this function to lookup a key value and return field values as a JSON encoded string. EXAMPLE: ZIP CODE LOOKUP - User enters ZIPCODE and we populate the CITY and STATE. IMPORTANT!: This example assumes that the ZIPCODE field name is identical in your current form/table and the ZIPCODES table. otherwise LOOKUPKEY would have to be set manually. $("#zipcode").blur(function(){ var lookupkey = $(this).attr("name"); var lookupvalue = $(this).val(); var valid_img = '<img src="/global_libs/images/icons/ checked.gif">'; var error_img = '<img src="/global_libs/images/icons/ unchecked.gif">'; $.post('lookup.php', {table : "zipcodes", key : lookupkey, value : lookupvalue, return_fields : "city,state" }, function(data){ eval(data); $("#city").val(data.city); $("#state").val(data.state); }); }); */ ?>