|
-
Feb 5th, 2010, 10:57 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] php with javascript? check if username is taken
i'm making a registration form and i want it to check if the username has been taken while the user is registering (before the user submits the form)
how to do this?
-
Feb 5th, 2010, 11:07 AM
#2
Re: php with javascript? check if username is taken
Just a quick thought
1) On the onBlur event of the username field
2) Use xmlhttprequest to fire your username to a php script (e.g. 'http://domain.com/checkUsername.php?username'+element.value)
3) Using PHP, check the usernames status, return this value
4) Depending on the result output either a success of failure message
Sorry I cannot provide an example right now, but Googling these stages should sort you out, post back with any problems you may have.
-
Feb 5th, 2010, 11:38 AM
#3
Thread Starter
Fanatic Member
Re: php with javascript? check if username is taken
thats a good tip, i will do some searching and return here
-
Feb 5th, 2010, 02:59 PM
#4
Fanatic Member
Re: php with javascript? check if username is taken
Hi,
I recently made an registration form, and checks for the existing username using AJAX. Try this.
Code:
function exists_username(field)
{
if (window.XMLHttpRequest)
{
XMLHttpRequestObject = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
}
field = field.value;
var url = "functions.php?ud="+ field;
if(XMLHttpRequestObject)
{
var obj = document.getElementById("v1"); //to display the error message as "Username already taken"
XMLHttpRequestObject.open("GET", url,false);
XMLHttpRequestObject.onreadystatechange = function()
{
if (XMLHttpRequestObject.readyState == 4 &&
XMLHttpRequestObject.status == 200)
{
if(XMLHttpRequestObject.responseText=="Username Already Exists")
{
obj.innerHTML = XMLHttpRequestObject.responseText;
return false;//but this is not returning as false.
}
return false;
}
}
XMLHttpRequestObject.send("");
}
}
functions.php
Code:
//connection to the host&database
$query = "SELECT username
FROM users
WHERE username='$ud';";
$result = mysqli_query($dbconn,$query);
if(mysqli_num_rows($result)>1)
{echo "Username Already Exists";}
-
Feb 5th, 2010, 09:44 PM
#5
Re: php with javascript? check if username is taken
I would personally advise you find a tutorial about ajax and use that instead of using any of the code posted above.
-
Feb 6th, 2010, 03:28 AM
#6
Thread Starter
Fanatic Member
Re: php with javascript? check if username is taken
yeah thats what i will do too, otherwise i won't learn much of it..
-
Feb 11th, 2010, 07:50 AM
#7
Re: php with javascript? check if username is taken
 Originally Posted by kows
I would personally advise you find a tutorial about ajax and use that instead of using any of the code posted above.
The code is also vulnerable to SQL injection; uses register globals and sends reduncdant information. Prehaps its best to delete the entire post.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|