i am a complete noob at javascript. i want to make an ajax username checker(checks if username is taken or not)
I want it to look like the one in the IPB registration form.
i found a link that does something like that, but you need to push a button to check the name.
heres the link
http://www.cyberdummy.co.uk/2005/07/18/gma...sername-signup/
any help?
Ajax help
Started by Uzamaki Itachi, Mar 27 2007 08:23 PM
4 replies to this topic
#1
Posted 27 March 2007 - 08:23 PM
#2
Posted 27 March 2007 - 09:24 PM
Read it. Lol thats all.
#3
Posted 28 March 2007 - 03:05 PM
i want it to check the username without clicking submit...
#4
Posted 28 March 2007 - 03:13 PM
hmm try this.
First we need to create a basic document with a few fields for user input:
Now that we have our document and JavaScript done lets more onto the PHP part []
Create a new file called check.php
Source: here
First we need to create a basic document with a few fields for user input:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Register</title>
<script type="text/javascript">
/*
This is our function to use xmlhttp requrests, i did not write this part w3 schools did
This was quicker than just making my own :)
*/
var xmlHttp // xmlHttp variable
function GetXmlHttpObject(){ // This function we will use to call our xmlhttpobject.
var objXMLHttp=null // Sets objXMLHttp to null as default.
if (window.XMLHttpRequest){ // If we are using Netscape or any other browser than IE lets use xmlhttp.
objXMLHttp=new XMLHttpRequest() // Creates a xmlhttp request.
}else if (window.ActiveXObject){ // ElseIf we are using IE lets use Active X.
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP") // Creates a new Active X Object.
} // End ElseIf.
return objXMLHttp // Returns the xhttp object.
} // Close Function
function CheckUsername(username){ // This function we will use to check to see if a username is taken or not.
xmlHttp=GetXmlHttpObject() // Creates a new Xmlhttp object.
if (xmlHttp==null){ // If it cannot create a new Xmlhttp object.
alert ("Browser does not support HTTP Request") // Alert Them!
return // Returns.
} // End If.
var url="check.php?username="+username // Url that we will use to check the username.
xmlHttp.open("GET",url,true) // Opens the URL using GET
xmlHttp.onreadystatechange = function () { // This is the most important piece of the puzzle, if onreadystatechange is equal to 4 than that means the request is done.
if (xmlHttp.readyState == 4) { // If the onreadystatechange is equal to 4 lets show the response text.
document.getElementById("usernameresult").innerHTML = xmlHttp.responseText; // Updates the div with the response text from check.php
} // End If.
}; // Close Function
xmlHttp.send(null); // Sends NULL instead of sending data.
} // Close Function.
</script>
</head>
<body>
<form>
<table cellpadding="" cellspacing="">
<tr>
<td width="100">Username</td>
<td><input type="text" name="username" onchange="CheckUsername(this.value);" /></td>
<td><div id="usernameresult"></div></td>
</tr>
</table>
</form>
</body>
</html>
Now that we have our document and JavaScript done lets more onto the PHP part []
Create a new file called check.php
<?php
if(!isset($_GET['username'])){ // If they are trying to view this without ?username=username or ?password=password.
die("What’s this document for?"); // Lawl what is this document for anyways?
}elseif(isset($_GET['username'])){ // ElseIf they are want to check their username.
require('config.php'); // Requires config.php so we can access the database.
$user=stripslashes(strip_tags(htmlspecialchars($_GET['username'], ENT_QUOTES))); // Cleans all nasty input from the username.
$check=mysql_num_rows(mysql_query("SELECT * FROM `users` WHERE `username` = '".$user."'")); // Checks to see if a user is in the `users` table or not.
if($check == 0){ // If there is no username in the database that matches the input one.
echo ''.$user.' is <span style="color:#009933">Available!</span>'; // Yay we can use it.
}elseif($check != 0){ // ElseIf there is a username in the database.
echo ''.$user.' is <span style="color:#CC0000">Not Available!</span>'; // None for you higgans.
} // End ElseIf.
} // End ElseIf.
?>
Source: here
#5
Posted 28 March 2007 - 04:22 PM
thanks!
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users
