Page 1 of 2
PHP / Ajax -- Username Availability
Posted: Wed Jun 21, 2006 12:31 am
by tecktalkcm0391
First what do you think is better a pop-up window to check user availability. or it to just do it and display if its availability. or not on the same page as the reg?
If I should do the Ajax this is what I have right now (pop-up window) to check the username:
Code: Select all
<?php
ob_start();
require('conf.inc.php');
$username = $_GET['username'];
$fname = $_GET['fname'];
print('<html><head><title>Username Availability</title></head><body>');
if(!empty($username)){
$query = mysql_query("SELECT Username from users WHERE Username = '$username'");
if(mysql_num_rows($query)>=1)
{
if(!empty($fname)){
print("<center><b>Sorry $fname,</b><br>Someone has already chosen the username <b>$username.</b> <br>Please try another username.");
} else {
print("<center><b>Sorry,</b><br>Someone has already chosen the username <b>$username.</b> <br>Please try another username.");
}
print('<br><br><a href="#" onClick="self.close()"><center>Close</center></a></body></html>');
ob_flush();
} else {
print("<center><b>Congradulations $fname!</b><br>The username<b> $username </b>has not yet been taken.");
print('<br><br><a href="#" onClick="self.close()"><center>Close</center></a></body></html>');
}
} else {
print("<center><b>Your must enter a username!</b><br>Your have not entered a username, or <br>your username is invaild.<br>Please try again.");
print('<br><br><a href="#" onClick="self.close()"><center>Close</center></a></body></html>');
}
ob_flush();
?>
How would I change it if I should do Ajax?
Posted: Wed Jun 21, 2006 12:42 am
by bdlang
I think that's beyond the scope of this forum; you should definitely read some
Ajax tutorials and maybe pick up a book on the subject. I personally like the method used by Gmail and other sites now check the availablity of a username and give you immediate feedback on the page, I'd use it if I anticipated a large user base.
Posted: Wed Jun 21, 2006 9:52 am
by tecktalkcm0391
Ok, that sounds good. I'll try out the ajax thing like Gmail.
Posted: Wed Jun 21, 2006 10:05 am
by d3ad1ysp0rk
On key press type events can be taxing performance wise, as well as sometimes annoying if not done right. They can however be very nice additions.
basically, add your javascript handling functions in your head
on key press (or whatever the accepted function is called) checkUsername(this.value) or something, which then tranfers that value to say.. checkuser.php?name=username, which returns 1 or 0. Swap out the value of your <span> element after that (say "Available" or "Unavailable"?), then use the same element object to swap it's font color from green to red or back?
Just some ideas.
Posted: Wed Jun 21, 2006 10:56 am
by bokehman
d3ad1ysp0rk wrote:On key press type events can be taxing performance wise
No necessarily. For example you could send a request to the server only once four characters have been press. The server returns all usernames that start with those four characters, (probably none). No further request to the server would be needed unless the client backtracked to less than four characters.
To be honest though I think an onblur handler would be better and a Javascript alert, not a pop up window to advise the client. The username selection will need to be rechecked server side for those without Javascript and in case the username was take in the time between the backgound check and the main form submission.
d3ad1ysp0rk wrote:add your javascript handling functions in your head
I couldn't agree less. Put all your Javascript, including event handlers in an external file. There should be absolutely no trace of javascript (not even event handlers) in your html document.
Think as Javascript like ketchup. The meal should be completely edible without it and people that are allergic to it should not find any trace of it on their plate.
Posted: Wed Jun 21, 2006 11:13 am
by Chris Corbyn
If you're using onKeyPress The XMLHttpRequest object will likely throw exceptions because it's busy in a transaction while it tries to send the next. Use a factory of some sort which churns out a new XMLHttpRequest object each time you send a request and also don't blindly do it on keypress.
People type FAST. Do this based on time since last keypress. So onkeypress set a timeout of say, 750ms to fire the request, on the next keypress clear that timeout and start it again. This way you'll only send off a request when the user stops typing (or pauses for a short while).
Posted: Wed Jun 21, 2006 11:20 am
by tecktalkcm0391
I haven't gotten anywhere with this but I am going to use onFocus... most likley. Anybody can find a site with something for me to read it would be awsome. Cause i've searched ajax user avaliablity, and ajax tutuorial. and i can't find anything that i understand.
Posted: Wed Jun 21, 2006 11:24 am
by Chris Corbyn
tecktalkcm0391 wrote:I haven't gotten anywhere with this but I am going to use onFocus... most likley. Anybody can find a site with something for me to read it would be awsome. Cause i've searched ajax user avaliablity, and ajax tutuorial. and i can't find anything that i understand.
That's because you're searching too specifically. Learn AJAX, then figure that bit out for yourself

There's loads on how to use AJAX.
Posted: Wed Jun 21, 2006 11:29 am
by tecktalkcm0391
Ok. I'll do that but one more question is there a way to suggest usernames if they are unavaliable like yahoo.com
Posted: Wed Jun 21, 2006 3:00 pm
by John Cartwright
It all depends on how you want to handle it.. I believe yahoo and hotmail and other provides try to combine your desired username with another piece of information you have given.
Posted: Wed Jun 21, 2006 3:50 pm
by Weirdan
http://ajaxpatterns.org/Main_Page has some very useful patterns and tips.
Posted: Mon Jun 26, 2006 8:40 am
by tecktalkcm0391
I have a user avaliability check, and I want to know what I should use to make an option (like Yahoo!'s Signup) where if the username was taken, then it shows other possible choices.
Posted: Mon Jun 26, 2006 8:42 am
by bokehman
A javascript alert.
Posted: Mon Jun 26, 2006 1:18 pm
by tecktalkcm0391
What? I don't get how that would help. I mean like if it type user as my username and its taken then it gives me back some choices like:
user1
user06
user_chris06
How would I do a code that generates the other suggested names
Posted: Mon Jun 26, 2006 1:22 pm
by John Cartwright
Code: Select all
$possibilities = array(
$username.rand(0,9),
$username.'_'.$firstname
);
you get the point..