Ajax confused

Status
Not open for further replies.

as4s1n

New Member
Messages
174
Reaction score
4
Points
0
I'm experimenting a little bit with Ajax and i'm trying to get an asynchronous login that checks the name when the user leaves the field (onblur). I'm totally lost, the ajax is fine but i'm trying to get the php page with the usernames on it working...

PHP:
$query = "SELECT login from users"; 
$result = mysql_query($query); 

while($row=mysql_fetch_array($result, MYSQL_ASSOC)){ 
 $login = $row['login']; 
  
$details = array ( 
 '$login' 
); 
} 

echo $details[$_REQUEST['ImageID']];
Obviously i'm referencing the details. I'm not sure how to set the $login as the array of taken names... any ideas?

it always comes back as unavailable which is what i told it to say when its taken...
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Rather than fetching all login names and checking a form field against the array, how about looking up the requested login name? If that fails, the username is available.
 

as4s1n

New Member
Messages
174
Reaction score
4
Points
0
so something like...

PHP:
$username = $_REQUEST['username'];
$query = "SELECT * from users where login = '$username'";
$result = mysql_query($query);

if(mysql_num_rows($result) == '0'){
 echo "Available";
}
else {
 echo "Unavailable";
}
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Almost. One important change (and 2 not as important ones):
PHP:
// important: make sure $username is safe to pass to query
$username = mysql_real_escape_string($_REQUEST['username']); 
// no need to get the whole row
$query = "SELECT login FROM users WHERE login='$username'";  
$result = mysql_query($query); 

$rowcount = mysql_num_rows($result);
if($rowcount === 0) {
  echo "Available";
} else if ($rowcount === False) {
  echo 'Error';
} else { 
  echo "Unavailable";
}
 

as4s1n

New Member
Messages
174
Reaction score
4
Points
0
Hmm, i've never heard of mysql_real_escape_string();

and i've never used the strictly equal to (===) successfully b4 o_O

and apparently thats not the problem o_O

i think it might be my js

HTML:
function checkUsername() {
 request = createRequest();
  if(request == null) {
   alert("unable to create request");
  } else {
   var theName = document.getElementById("username").value;
   var username = escape(theName);
   var url = "checkUser?username="+username;
   request.onreadystatechange = showUsernameStatus;
   request.open("GET", url, true);
   request.send(null);
  }
 }
 
 function showUsernameStatus(){
  if(request.readyState == 4){
   if(request.status == 200){
    if(request.responseText == "okay"){
	 document.getElementById("usernameHelp").innerHTML = "<span style='color:green;'>Available</span>";
	 document.getElementById("send").disabled = false;
	 }
	else {
	 document.getElementById("usernameHelp").innerHTML = "<span style='color:red;'>Unavailable</span>";
	 document.getElementById("send").disabled = true;
	}
   }
  }
 }

<input type="text" id="username" size="12" onkeyup="checkUsername()" /><div id="usernameHelp"></div>
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Well, your PHP script returns "Available", but your JS checks for "okay".

Also, rather than checking the username immediately upon keyup, you might want to schedule the check on a timer (plus an immediate check upon blur).
 

as4s1n

New Member
Messages
174
Reaction score
4
Points
0
Thanks it works perfectly now ^.^

i have one more question...

about the (===). i never use it right for some reason, is it just like the = & == or is there some special condition you have to do b4 it works?
 

Livewire

Abuse Compliance Officer
Staff member
Messages
18,169
Reaction score
216
Points
63
Thanks it works perfectly now ^.^

i have one more question...

about the (===). i never use it right for some reason, is it just like the = & == or is there some special condition you have to do b4 it works?

=== is identical to == except it also checks the datatype. Good example is boolean false and string false (or bool true and string true).


In code:

$bool_false=false;
$string_false="false";

($bool_false==$string_false) //this evaluates to true
($bool_false===$string_false) //this evaluates to false

It's a nice way to evaluate on functions that return a string, or false on failure. Should the function return the string "false," you'll have problems if you aren't using === :)
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
$bool_false=false;
$string_false="false";

($bool_false==$string_false) //this evaluates to true

false != "false". I think you mean something like '' == false and '' !== false. Also, 10 == "010" && 0 == '' && null == '' but 10 !== "010" && 0 !== '' && null !== ''.
 
Status
Not open for further replies.
Top