PHP / Ajax -- Username Availability

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

PHP / Ajax -- Username Availability

Post 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?
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post 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.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Ok, that sounds good. I'll try out the ajax thing like Gmail.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post 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.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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).
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

http://ajaxpatterns.org/Main_Page has some very useful patterns and tips.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

A javascript alert.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

$possibilities = array(
   $username.rand(0,9),
   $username.'_'.$firstname
);
you get the point..
Post Reply