Hi friends i m new to to this site any help will be greatly appreciated I am making a sign up form in php and i am using a username availabilty feature there using jsp and ajax when i enter a username In textbox its showing that the username is available or not but when i am submitting the value the not available username is also submited to the database...π can anyone pls help me out...here the coding....
check.js
var request;
function createRequest()
{
try{
request=new XMLHttpRequest();
}catch(e){
try{
request=new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
request=new ActiveXObject("Microsoft.XMLHTTP");
}
}return request;
}
function CheckUsername(username){
request=createRequest()
if (request==null){
alert ("Browser does not support HTTP Request")
return
}
var url="check.php?username="+username
request.open("GET",url,true)
request.onreadystatechange = function () {
if (request.readyState == 4) {
document.getElementById("usernameresult").innerHTML = request.responseText;
}
};
request.send(null);
}
check.php
<?php
$con= mysql_connect('localhost', 'root');
if (!$con) {
die('Could not connect to database server: ' . mysql_error());
}
$dbname = 'my_db';
$db_selected = mysql_select_db($dbname, $con);
if (!$db_selected) {
die("Could not connect $dbname " . mysql_error());
}
$user=$_GET[username];
$check=mysql_num_rows(mysql_query("SELECT * FROM user WHERE username ='$user'")); // Checks to see if a user is in the users table or not.
if($check == 0){
echo 'username available';
}elseif($check != 0){
echo 'username not available';
}
?>
signup.php
<?php
$con=mysql_connect("localhost","root");
if(!$con)
{
echo "db connect failure".mysql_error();
}
mysql_select_db("my_db");
?>
<body><head><script type='text/javascript'>
function formValidator(){
// Make quick references to our fields
var name = document.getElementById('name');
var age = document.getElementById('age');
var addr = document.getElementById('addr');
var phne = document.getElementById('phne');
var username = document.getElementById('username');
var email = document.getElementById('email');
var pass = document.getElementById('pass');
var pass1 = document.getElementById('pass1');
// Check each input in the order that it appears in the form!
if(isAlphabet(name, "Please enter only letters for your name")){
if(isNumeric(age, "Please enter numbers only")){
if(isAlphanumeric(addr, "Numbers and Letters Only for Address")){
if(isNumeric(phne, "Please enter numbers only")){
if(emailValidator(email, "Please enter a valid email address")){
if(lengthRestriction(username, 6, 20)){
if(madeSelection(pass,"password field cannot be blank")){
if(madeSelection(pass1,"confirm password field cannot be blank")){
return true;
}
}
}
}
}
}
}
}
return false;
}
function notEmpty(elem, helperMsg){
if(elem.value.length == 0){
alert(helperMsg);
elem.focus(); // set the focus to this input
return false;
}
return true;
}
function isNumeric(elem, helperMsg){
var numericExpression = /[0-9]+$/;
if(elem.value.match(numericExpression)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
function madeSelection(elem, helperMsg){
if(elem.value == ""){
alert(helperMsg);
elem.focus();
return false;
}else{
return true;
}
}
function isAlphabet(elem, helperMsg){
var alphaExp = /[a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
function isAlphanumeric(elem, helperMsg){
var alphaExp = /[0-9a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
function lengthRestriction(elem, min, max){
var uInput = elem.value;
if(uInput.length >= min && uInput.length <= max){
return true;
}else{
alert("Username should be between " +min+ " and " +max+ " characters");
elem.focus();
return false;
}
}
function emailValidator(elem, helperMsg){
var emailExp = /[\w-.+]+@[a-zA-Z0-9.-]+.[a-zA-z0-9]{2,4}$/;
if(elem.value.match(emailExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
</script>
<script language="javascript">
function DoCustomValidation()
{
var frm = document.forms["myform"];
if(frm.pd.value != frm.pd1.value)
{
alert('The Password and confirm password does not match!');
return false;
}
else
{
return true;
}
}
</script>
<script type="text/javascript" src="check.js">
</script>
</head>
<table align="center" cellpadding="3" cellspacing="4" >
<form action="" method="post" name="myform" target="self" onsubmit="return formValidator()" onClick="return DoCustomValidation()">
<caption><img src="signup_Now_icon.jpg" border="0"></caption>
<tr><td><B>Name:</td><td><input type="text" name="name" id="name"></td></tr>
<tr><td><B>Age:</td><td><input type="text" name="age" id="age"></td></tr>
<tr><td><B>Address:</td><td><input type="text" name="ad" id="addr"></td></tr>
<tr><td><B>PhoneNo:</td><td><input type="text" name="pn" id="phne"></td></tr>
<tr><td><B>Email:</td><td><input type="text" name="email" id="email"></td></tr>
<tr><td width="100"><b>Username</td>
<td><input type="text" name="username" id="username" onBlur="return CheckUsername(this.value);" /></td>
<td><div id="usernameresult" style="color:#FF9900"></div></td></tr>
<tr><td><B>Password:</td><td><input type="password" name="pd" id="pass"></td></tr>
<tr><td><B>Confirm Password:</td><td><input type="password" name="pd1" id="pass1"></td></tr>
<tr><td><input type="submit" name="submit" value="SignUp" title="click here to SignUp"></td></tr>
</table>
<br />
<br />
<?php
if(isset($POST[submit]) && $POST[submit]!='')
{
$firstname=$POST[name];
$sql="INSERT INTO user (name,age,address,phneno,email,username,password)
VALUES
('$POST[name]','$POST[age]','$POST[ad]','$POST[pn]','$POST[email]','$POST[username]','$_POST[pd]')";
if (!mysql_query($sql,$con))
{
echo "<b style=color:#FFCC33>";
echo ('Sorry!cannot register u at this time'.mysql_error() );
}
else
{
echo "<center>";
echo "<b style=color:#FF9900>";
echo "Hi $firstname ! You are successfully registered";
?>
<p align="center" style="color:#FFCC00"><a style="color:#FF9900" href="index.php">click here to LogIn</a></p>
<br>
<br>
<?php
mysql_close($con);
exit();
}
}
?>
</form>
</body>
pls help meπππ