SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2011
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:
    Code:
    $('#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
    Code:
    <?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
    }
    
    ?>

  2. #2
    SitePoint Enthusiast
    Join Date
    Oct 2012
    Posts
    41
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •