Jquery and php username validation not working?

Discussion in 'jQuery' started by Anveto, Oct 25, 2011.

  1. #1
    I am trying to make sure the user does not input an existing username on the form but I am not getting it to display anything.

    This is part of my index file, I have included jquery.js and login.js in the header

    <td height="30"><label for="username">Username:</label></td>
          <td><input type="text" name="username" id="username" /> 
          </td>
          <td><span id="usernameLoading"><img src="images/indicator.gif" alt="Ajax Indicator" /></span>
    <span id="usernameResult"></span></p></td>
    Code (markup):
    login.js

    $(document).ready(function() {
    	$('#usernameLoading').hide();
    	$('#username').blur(function(){
    	  alert("IT DID SOMETHING?");
    	  $('#usernameLoading').show();
          $.post("../functions/checkuser.php", {
            username: $('#username').val()
          }, function(response){
            $('#usernameResult').fadeOut();
            setTimeout("finishAjax('usernameResult', '"+escape(response)+"')", 400);
          });
        	return false;
    	});
    });
    function finishAjax(id, response) {
      $('#usernameLoading').hide();
      $('#'+id).html(unescape(response));
      $('#'+id).fadeIn();
    } //finishAjax
    Code (markup):
    checkuser.php

    <?php
    include("db.php");
    $username = $_POST['username'];  
    $username = trim(htmlentities($username));
    
    echo check_username($username); // call the check_username function and echo the results.
    
    function check_username($username){
    	$result = "SELECT * FROM User WHERE Username='$username'"; 
    	$r = mysql_query($result);
    		if(mysql_num_rows($r) >= 1){
    			return '<span style="color:#f00">Username Unavailable</span>';
    		}
    	
    	return '<span style="color:#0c0">Username Available</span>';
    }
    ?>
    PHP:
    Thanks for any help, im really stuck here.
     
    Anveto, Oct 25, 2011 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    
    <?php
    include("db.php");
    $username = $_POST['username'];
    
    echo $username.'<br />';
    
    $username = trim(htmlentities($username));
    
    echo check_username($username); // call the check_username function and echo the results.
    
    function check_username($username){
        $result = "SELECT * FROM User WHERE Username='$username'"; 
        $r = mysql_query($result);
            if(mysql_num_rows($r) >= 1){
                return '<span style="color:#f00">Username Unavailable</span>';
            }
        
        return '<span style="color:#0c0">Username Available</span>';
    }
    ?>
    
    PHP:
    See whether the php file is ever called and whether the username is sent to it.

    (If you're going to write PHP code, learn to use FirePHP. It makes debugging a lot easier.)
     
    Rukbat, Oct 25, 2011 IP
  3. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #3
    Thanks, I got firebug already and now i've downloaded firephp :)

    but I think the problem might be in the jquery part of the file, see if I use the code above I never get an alert no matter what i did

    $('#username').blur(function(){
    	  alert("IT DID SOMETHING?");
    	  $('#usernameLoading').show();
          $.post("../functions/checkuser.php", {
    Code (markup):
    But if I change to this i get the alert

    
    
    	  alert("IT DID SOMETHING?");
    $('#username').blur(function(){\
    	  $('#usernameLoading').show();
          $.post("../functions/checkuser.php", {
    Code (markup):
     
    Anveto, Oct 25, 2011 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    I ran your code from post #1 (checkuser.php just echos that the user is okay) and it runs fine. All I had to add was the script tags (and I included the jQuery in the head of the document, not in a separate file) and and the link to jQuery.
     
    Rukbat, Oct 25, 2011 IP
  5. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #5
    Thanks, just wanted to say i got it working
     
    Anveto, Oct 26, 2011 IP