Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Why Javascript variable not working?

    HI

    Why found variable not working properly in if statement?
    The found variable working properly in if ( user.length < 6 || user.length > 20 ) but not working in if(code <=0). why? i want to send found value via return at the end of the function username_check()

    please anyone resolve this.


    Code:
    function pullAjax(){
        var a;
        try{
          a=new XMLHttpRequest()
        }
        catch(b)
        {
          try
          {
            a=new ActiveXObject("Msxml2.XMLHTTP")
          }catch(b)
          {
            try
            {
              a=new ActiveXObject("Microsoft.XMLHTTP")
            }
            catch(b)
            {
              alert("Your browser broke!");return false
            }
          }
        }
        return a;
      }
    
    
      function username_check()
      {
    
    	var found = 0;
    
        var x = document.getElementById('username');
        var username_msg = document.getElementById('username_msg');
        user = x.value;
     
        code = '';
        message = '';
    
        obj=pullAjax();
        obj.onreadystatechange=function()
        {
    
    if ( user.length < 6 || user.length > 20 )
    	{
              x.style.border = "2px solid red";
              username_msg.style.color = "red";
    		  message = "Username contains min 6 and max 20 allowed characters";
              username_msg.innerHTML = message;
    		  found = 1;   //This works properly
    	}
    
          else if(obj.readyState==4)
          {
            eval("result = "+obj.responseText);
            code = result['code'];
            message = result['result'];
     
            if(code <=0)
            {
              x.style.border = "2px solid red";
              username_msg.style.color = "red";
              found = 1;     //This doesnt works and found value remain zero
            }
            else
            {
              x.style.border = "2px solid green";
              username_msg.style.color = "green";
            }
            username_msg.innerHTML = message;
          }
        
    	}
    
        obj.open("GET",site_root+"username_check.php?username="+user,true);
        obj.send(null);
    	
    return found;
      }
    Last edited by moin1407; 03-05-2011 at 04:49 PM.

  • #2
    Regular Coder
    Join Date
    Mar 2011
    Posts
    148
    Thanks
    0
    Thanked 20 Times in 20 Posts
    Try to force code to be a number
    var code = result['code'] * 1;
    or
    var code = parseFloat(result['code']);
    or
    if(code.length<=0)
    Last edited by MarPlo; 03-05-2011 at 05:03 PM.

  • #3
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi

    Thanks

    These are not working also
    actually code is variable which comes from username_check.php

    Here is username_check.php

    PHP Code:

    <?php

    $user 
    strip_tags(trim($_REQUEST['username']));
     
    if(
    strlen($user) <= 0)
    {
      echo 
    json_encode(array('code'  =>  -1'result'  =>  'Invalid username, please try again.'));
      die;
    }
     
        
    $link mysql_connect('localhost''root''');
        
    mysql_select_db('results'$link);

    $query mysql_fetch_array(mysql_query("Select * from `USERS` where username = '$user' "));
     
     
    if( 
    $query == "" )
    {
      echo 
    json_encode(array('code'  =>  1'result'  =>  "Success,username $user is still available"));
      die;
    }
    else
    {
      echo  
    json_encode(array('code'  =>  0'result'  =>  "Sorry but username $user is already taken."));
      die;
    }

    ?>


  •  

    Posting Permissions

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