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?
Printable View
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?
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.
thats a good tip, i will do some searching and return here :)
Hi,
I recently made an registration form, and checks for the existing username using AJAX. Try this.
functions.phpCode: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("");
}
}
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";}
I would personally advise you find a tutorial about ajax and use that instead of using any of the code posted above.
yeah thats what i will do too, otherwise i won't learn much of it..