Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-05-2011, 04:47 PM   PM User | #1
moin1407
New to the CF scene

 
Join Date: Mar 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
moin1407 is an unknown quantity at this point
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..
moin1407 is offline   Reply With Quote
Old 03-05-2011, 04:58 PM   PM User | #2
MarPlo
Regular Coder

 
Join Date: Mar 2011
Posts: 145
Thanks: 0
Thanked 20 Times in 20 Posts
MarPlo is an unknown quantity at this point
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..
MarPlo is offline   Reply With Quote
Old 03-05-2011, 05:16 PM   PM User | #3
moin1407
New to the CF scene

 
Join Date: Mar 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
moin1407 is an unknown quantity at this point
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;
}

?>
moin1407 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:32 PM.


Advertisement
Log in to turn off these ads.