...

View Full Version : Why Javascript variable not working?



moin1407
03-05-2011, 05:47 PM
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.




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;
}

MarPlo
03-05-2011, 05:58 PM
Try to force code to be a number
var code = result['code'] * 1;
or
var code = parseFloat(result['code']);
or
if(code.length<=0)

moin1407
03-05-2011, 06:16 PM
Hi

Thanks

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

Here is username_check.php




<?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;
}

?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum