Check SQL Table

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

Post Reply
Trahb
Forum Commoner
Posts: 36
Joined: Sat Jan 30, 2010 9:09 pm

Check SQL Table

Post by Trahb »

I'm creating a registration page and onchange, I would like it to check the SQL database to see if the username is already in use, and I understand I'll need AJAX for this, but I'm not quite sure how.

Any ideas? |:
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Check SQL Table

Post by Christopher »

You don't need Ajax for that, but you can use it. You just need to do a SELECT when they submit the form. If it exists then respond with an error status. If it does not exist then create the record and respond with a success status.
(#10850)
Trahb
Forum Commoner
Posts: 36
Joined: Sat Jan 30, 2010 9:09 pm

Re: Check SQL Table

Post by Trahb »

arborint wrote:You don't need Ajax for that, but you can use it. You just need to do a SELECT when they submit the form. If it exists then respond with an error status. If it does not exist then create the record and respond with a success status.

My bad I left some stuff out D: I know how to do that much; at the moment, I have it check with a PHP function whether or not it exists, but I'd like for a green button to appear if it does not exist, and a red one to indicate it's taken.

Sorry for that ambiguity.
limitdesigns
Forum Commoner
Posts: 25
Joined: Sat Feb 06, 2010 9:05 pm

Re: Check SQL Table

Post by limitdesigns »

So, have the onblur() event of the username text field call an AJAX function which returns either '' (for username is available) or some error message (like, "Username is already in use"). Then, have a check in the javascript: if the AJAX script returns '', then you can change the button's color to green. If it has something (the error), then you can change the button's color to red.

If you aren't using it already, jQuery is perfect for this sort of thing. I just did this myself on a website I'm developing for and it works perfectly, and it took only 15 minutes.
Trahb
Forum Commoner
Posts: 36
Joined: Sat Jan 30, 2010 9:09 pm

Re: Check SQL Table

Post by Trahb »

Well I looked into some jquery tutorials and didn't quite find any ways to do what I'm looking for ( probably cause they're all beginner tuts )

Would you care to go into a little more detail? :P
Post Reply