hi my name is shan and I am new to AJAX
with JQuery
& PHP
. I am trying to look for usernames
that have been already taken, if it is already taken then I want it to print username
taken, else not taken. since I have also written please type a username
in case of it being blank. And firebug is also not throwing any errors. Any ideas guys???
My PHP code is as follows
<?php
include 'dbconfig.inc.php';
@$uname= htmlentities($_POST['unamer']);
$uname_val= mysql_real_escape_string($uname);
$row =$project->viewByUname($uname_val);
if ($row>0) {
#uname is taken
echo 0;
}
else {
#uname is not taken
echo 1;
}
JQuery code:
$.post("includes/uname_val.php", {unamer:uname_val},
function(result) {
if(result === 1) {
//the uname is available
console.log("working now");
$(".js-write_u_r").html('<span class="glyphicon glyphicon-ok"> Username Is Available</span>');
return true;
}
else if(result===0) {
console.log("working now1");
//the uname is not available
$(".js-write_u_r").html('<span class="glyphicon glyphicon-remove"> Username is not Available.</span>');
return false;
}
else if (uname_val===""){
console.log("working now but blank");
$(".js-write_u_r").html('Please type a Username.');
return false;
} else
{
console.log(" now not working");
return false;
}
HTML:
<div class="col-xs-1"></div><div class="col-xs-5"><h2>Register here!</h2><form action="includes/register.inc.php" method="post"><table>
<tr><td><input type="text" name="fnamer" placeholder="Firstname"required="please enter your first name" class="input-custom input-group-lg input-group-sm input-lg input-sm"></td></tr>
<tr><td><input type="text" name="lnamer" placeholder="Lastname"required="" class="input-custom input-group-lg input-group-sm input-lg input-sm"></td></tr>
<tr><td><input type="text" name="unamer" placeholder="Username" required="" class="input-custom input-group-lg input-group-sm input-lg input-sm uname-val"></td>
<td><p class="js-write_u_r"></p></td></tr>
<tr><td><input type="password" name="passr" placeholder="Password"required="" id="pass0"class="input-custom input-group-lg input-group-sm input-lg input-sm pass0"></td></tr>
<div> </div><tr><td><input type="password" name="pass1r" placeholder="Retype Password" required=""id="pass1" class="input-custom input-group-lg input-group-sm input-lg input-sm pass1" ></td>
<td><p class="js-write"></p></td></td></tr>
<br><tr><td><input type="email" name="emailr" placeholder="email" required="" class="input-custom input-group-lg input-group-sm input-lg input-sm"></td></tr>
<br><tr><td><input type="text" name="phoner" placeholder="Phone Number" required="" class="input-custom input-group-lg input-group-sm input-lg input-sm"></td></tr>
<br><tr><td><br><button type="submit" name="submit-register" value="login" class="btn btn-group-lg btn-danger btn-lg">Register</button></td></tr>
</table></form></div><br><div> </div>
now it output's "now not working" in console it gives a clean 200 response @Disha
Try to use ==
instead of ===
.===
will compare also datatype of the return value . And in ajax response you will alwase get the response as a string weather you are echoing integer or string .
$.post("includes/uname_val.php", {unamer:uname_val},
function(result) {
if(result == 1) {
//the uname is available
console.log("working now");
$(".js-write_u_r").html('<span class="glyphicon glyphicon-ok"> Username Is Available</span>');
return true;
}
else if(result== 0) {
console.log("working now1");
//the uname is not available
$(".js-write_u_r").html('<span class="glyphicon glyphicon-remove"> Username is not Available.</span>');
return false;
}
else if (uname_val== ""){
console.log("working now but blank");
$(".js-write_u_r").html('Please type a Username.');
return false;
} else
{
console.log(" now not working");
return false;
}
Or you need to compare it with string like
if(result === '1')
{
//the uname is available
console.log("working now");
$(".js-write_u_r").html('<span class="glyphicon glyphicon-ok"> Username Is Available</span>');
return true;
}