9 Replies - 1811 Views - Last Post: 28 October 2013 - 03:03 AM

#1 poliosynthesis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 27-June 13

Script returning "Username is taken" when it isn't

Posted 24 October 2013 - 07:14 AM

Hi,
I am writing a login form where the script validates itself as the user enters their details. When entering a username, the script first checks whether the name is longer than four characters, then checks if the name is already registered on the database. Everything works fine, except the error message that tells the user that the name is taken, it displays even if the name isn't registered(there are currently no names on the database).

index.php
<html>
<head>
<link rel="stylesheet" href="css/ui-lightness/jquery-ui-1.10.3.custom.css" />
<script src="js/jquery-1.9.1.js"></script>
<script src="js/jquery-ui-1.10.3.custom.min.js"></script>
<link type="text/css" href="general.css" rel="stylesheet" />
<script type="text/javascript" src="external.js"></script>
</head>
<body>
<div id="container">
<form id="customForm">
 <div>
   <label for="name">Name:</label>  
   <input id="name" name="name" type="text">
<span id="nameInfo">What's your name?</span> 
     </div>
<div>
 <label for="email">Email:</label>  
   <input id="email" name="email" type="text">
<span id="emailInfo">Please enter a valid email</span> 
      
     </div>
<div>
 <label for="password">Password:</label>  
   <input id="password" name="password" type="password">
<span id="passInfo">Please enter a password over 5 characters</span> 
</div>
<div>
       <label for="cpassword">Confirm Password:</label>  
   <input id="cpassword" name="cpassword" type="password">
<span id="cpassInfo">Please confirm password</span> 
     </div>
<div>
    <input id="send" name="send" type="button" value="Send" /> 
     </div>
</form>
</div>


</div>
</body>
</html>


external.js

$(document).ready(function(){

var form = $("#customForm");
var name = $("#name");
var nameInfo = $("#nameInfo");
var email = $("#email");
var emailInfo = $("emailInfo");
var password = $("#password");
var passInfo = $("#password");
var cpassword = $("#cpassword");
var cpassInfo = $("cpassInfo");
var state = false;
name.keyup(validateName);

	function validateName(){
		if(name.val().length <=  4){
		name.removeClass("valid");
		nameInfo.removeClass("valid");
		name.addClass("error");
		nameInfo.addClass("error");
		nameInfo.text("Please enter a name longer than 4 			characters");
		state = false;
		
		}else{
		if(name.val().length >4) {
		var uname  = name.val();
			$.post("external.php", {names:uname},function(data){

				if(data!="0"){
				name.removeClass("valid");
				nameInfo.removeClass("valid")
				name.addClass("error");
				nameInfo.addClass("error");
				nameInfo.text("This name is already registered");
				state = false;
				}else{ 
				name.removeClass("error");
				nameInfo.removeClass("error")
				name.addClass("valid");
				nameInfo.addClass("valid");
				nameInfo.text("Name available");
			  	state = true;
				}
			});
		}
		
		}
return state;
	}


});


external.php
<?php

$name = $_POST['names'];
$email = $_POST['emails'];


if($name!==""){

mysql_connect("localhost" , "root","") or die("Could not connect to database at this time.");
mysql_connect_db("testsite");
$username = mysql_query("SELECT Name FROM users WHERE Name='$name'");
$count = mysql_num_rows($username);
if($count!==""){
echo'1';

}else{

echo'0':
}
}


if($email!==""){

mysql_connect("localhost" , "root","") or die("Could not connect to database at this time.");
mysql_connect_db("testsite");
$useremail = mysql_query("SELECT Email FROM users WHERE Email='$email'");
$countemail = mysql_num_rows($useremail);
if($countemail!==""){
echo'1';

}else{

echo'0':
}
}
?>



Is This A Good Question/Topic? 0
  • +

Replies To: Script returning "Username is taken" when it isn't

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,254
  • Joined: 08-June 10

Re: Script returning "Username is taken" when it isn't

Posted 24 October 2013 - 07:23 AM

external.php:29, $countemail is a number and therefore never strict equal to an empty string.
Was This Post Helpful? 1
  • +
  • -

#3 poliosynthesis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 27-June 13

Re: Script returning "Username is taken" when it isn't

Posted 25 October 2013 - 04:29 AM

I changed it, I still have the same problem. Any other ideas?
Was This Post Helpful? 0
  • +
  • -

#4 Peter O  Icon User is offline

  • D.I.C Head

Reputation: 77
  • View blog
  • Posts: 182
  • Joined: 19-October 13

Re: Script returning "Username is taken" when it isn't

Posted 25 October 2013 - 04:42 AM

What did you change to? You have the same problem on line 13.
Was This Post Helpful? 0
  • +
  • -

#5 poliosynthesis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 27-June 13

Re: Script returning "Username is taken" when it isn't

Posted 25 October 2013 - 05:06 AM

I changed it to be "==0" on lines 29 and 13, still the same result.
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,254
  • Joined: 08-June 10

Re: Script returning "Username is taken" when it isn't

Posted 25 October 2013 - 05:14 AM

wouldn’t it be easier to echo the result of mysql_num_rows() directly?
Was This Post Helpful? 0
  • +
  • -

#7 Peter O  Icon User is offline

  • D.I.C Head

Reputation: 77
  • View blog
  • Posts: 182
  • Joined: 19-October 13

Re: Script returning "Username is taken" when it isn't

Posted 25 October 2013 - 05:19 AM

So external.php returns "1" if the username was not found in the database? external.js will treat that as an error.
Was This Post Helpful? 0
  • +
  • -

#8 poliosynthesis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 27-June 13

Re: Script returning "Username is taken" when it isn't

Posted 25 October 2013 - 05:41 AM

So should it be echoing "0"?
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3517
  • View blog
  • Posts: 12,008
  • Joined: 12-December 12

Re: Script returning "Username is taken" when it isn't

Posted 27 October 2013 - 11:51 AM

$email = $_POST['emails'];

Your form-element is named 'email', not 'emails'. Added: In fact, I don't think you are posting this information to external.php.

And you've dropped the hash-sign in here:

var cpassInfo = $("cpassInfo");

You've done this a few times.

Check your browser's console for other errors.

This post has been edited by andrewsw: 27 October 2013 - 11:57 AM

Was This Post Helpful? 0
  • +
  • -

#10 poliosynthesis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 27-June 13

Re: Script returning "Username is taken" when it isn't

Posted 28 October 2013 - 03:03 AM

Thanks very much! I managed to resolve all the errors and it now works.
Thanks again guys :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1