How to use the Jquery validate plugin's remote function

I’m trying to use the Jquery validate plugin’s remote function to check that a username is not already in use. But it doesn’t seem to be making it to the php. I have tried changing the path. Right now the form is located in the main directory with the validate file in a js folder and the php in a scripts folder.

Here is my js:


$('#contact-form').validate({
	
	rules: {
	
	    username:
                {
                    minlength: 2,
	        		required: true,
                    remote: "scripts/check_username.php"
                }
		},
	    messages: {

	      username: {
	        minlength: "username must be at least 2 characters",
	        required: "Enter a username ",
	        remote: jQuery.format("{0} is already in use")
	      }
		},
	    highlight: function(label) {
	    	$(label).closest('.control-group').addClass('error');
	    },
	    success: function(label) {
	    	label
	    		.text('OK!').addClass('valid')
	    		.closest('.control-group').addClass('success');
	    }
	
	  });

php - scripts/check_username.php


<?php
session_start();
require_once 'app_config.php';
require_once 'database_connection.php';

if(!empty($_POST['username'])
{
			
	
    $username = mr_clean($_POST['username']);
    //echo "email: ".$email;

    $query = sprintf("select * from users where username = '%s';", $username);

    $result = mysql_query($query) or handle_error(mysql_error());
    $num_rows = mysql_num_rows($result);

    if($num_rows == 0)
    {
        echo "true";  //good to register
    }
    else
    {
        echo "false"; //already registered
    }
}
else
{
    echo "false"; //invalid post var
}

?>

Have you inspected the AJAX request using FireBug (or any other tool)?

  • Is it being sent?
  • What are the parameters?
  • What is the response?

At first sight, there doesn’t seem to be anything wrong with your code, that’s why I’m asking the questions above.