jquery .post()与PHP无法正常工作以检查用户名是否可用

时间:2015-09-07 06:28:36

标签: php jquery .post

我的名字是shan,我是AJAX的新人,JQuery& PHP。我正在尝试查找已经拍摄的usernames,如果已经拍摄,那么我希望它print username拍摄,否则不拍摄。因为我还写了please type a username,因为它是空白的。萤火虫也没有抛出任何错误。任何想法的人???

我的PHP代码如下

<?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代码:

   $.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>

现在它在控制台输出“现在不工作”它给出了一个干净的200响应@Disha

1 个答案:

答案 0 :(得分:1)

尝试使用==代替======也会比较返回值的数据类型。并且在ajax响应中,您将获得响应作为字符串天气您回显整数或字符串。

$.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;
                }

或者您需要将其与字符串

进行比较
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;
 }