0

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>&nbsp;</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>&nbsp;</div>

now it output's "now not working" in console it gives a clean 200 response @Disha

9
  • Are you getting ajax response successfully? have you tried console log result? Commented Sep 7, 2015 at 6:37
  • you are using return in the ajax. And the return will work only if you are using callback function Commented Sep 7, 2015 at 7:37
  • after deleting return st's i still get the output "now not working" @ShowStopper . Commented Sep 7, 2015 at 7:44
  • have you tried to alert result?? Commented Sep 7, 2015 at 7:45
  • @ShowStopper i tried alert still it outputs "now not working" Commented Sep 7, 2015 at 7:50

1 Answer 1

1

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;
 }
Sign up to request clarification or add additional context in comments.

1 Comment

Finally after using == instead of === it is working now guys, thanks 4 d answers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.