Ajax接收错误的数据,但PHP工作正常

时间:2017-04-08 13:31:31

标签: php jquery ajax

当我使用action =""正常地将表单提交给PHP时......它可以工作。但是当我使用AJAX提交时,它会一直警告" ILD" ...无效的记录详细信息。 我做错了什么?

**这是我的表格**

 <form action="" method="post" onsubmit="return do_signIn()">
                        Username:
                        <input id="#user" type="text" class="form-control"  placeholder="Username" name="username" />
                        Password:
                        <input id="#pass" type="password" class="form-control"  placeholder="Password" name="password" />
                        <br />
                        <input id="login" type="submit" class="btn btn-danger" value="Login" name="login"/> <span><a href="#"><i class="fa fa-lock"></i> Forget Password?</a></span>
                        <br /><br />
                        <p id="error-area"></p>
                    </form>

**下面是我的AJAX JQUERY **

    <script type="text/javascript">
        function do_signIn() {
            var username = $("#user").val();
            var password = $("#pass").val();
            var login = $("#login").val();
            if(username!="" && password!=""){
                $("#error-area").html("<i class='fa fa-spinner fa-spin'></i> Loading...");
                $.ajax
            ({
            type:'post',
            url:'b-sign-in.php',
            data:{
                    do_signIn:"b-sign-in",
                    username:username,
                    password:password,
                    login:login,
            },
                    success:function(response) {
            if(response=="VA"){
                    alert("Verify Your Account" + response);
                    //window.location.href="";
            }else if(response=="SUCCESS"){
                    alert("Success! You are In");
                    //window.location.href="";
            }else if(response=="ELI"){
                    alert("Error Loggin In");
                    //window.location.href="";
            }else if(response=="ILD"){
                    alert(response);
                    //window.location.href="";
            }else{
            $("#error-area").html("Something Went Wrong");
            alert(response);
            //$("#error-area").slideUp(5000);
            }
            }
            });
            }else{
            $("#error-area").html("Please fill all fileds!");
            //$("#error-area").slideUp(5000);
            }       
            return false;
            }
</script>

和**这是我的PHP **

   if(isset($_POST['login']) && $_POST['login']=="Login"){      
        //check user input
        $username = sanitizeMySQL($con, $_POST['username']);
        $password = sanitizeMySQL($con, $_POST['password']);

        //harsh the password
        //Harsh the password
        $harshQuery = "SELECT info_value FROM b_info WHERE info_name = 'siteHarsh'";
        $harshRun = mysqli_query($con, $harshQuery);
        $harshRow = mysqli_num_rows($harshRun);
        if(!$harshRow || $harshRow > 1 || $harshRow < 1){
            echo "error"; // Show An unexpected error occured
            exit();
        }else{
            $passHarsh = mysqli_fetch_assoc($harshRun);
            $harsh = $passHarsh['info_value'];
            $correctPassword = md5($harsh . $password . $harsh);
        }

        $run = "
                    SELECT * FROM b_user
                    WHERE user_name = '$username'
                    AND user_password = '$correctPassword'
                ";
        $result = mysqli_query($con, $run);
        $rowno = mysqli_num_rows($result);

        //Check if detail match in db
        if($rowno == 1){
            $row = mysqli_fetch_array($result);
            $_SESSION['loggeduser'] = TRUE;
            $_SESSION['username'] = $row['user_name'];
            $_SESSION['email'] = $row['user_email'];

            if($row['user_activation'] == 0){
                echo "VA"; //Verify Your Account By Entering a Valid Phone Number
                exit();
            }else if($row['user_activation'] == 1){
                echo "SUCCESS";
                exit();
            }
            else{
                echo "ELI"; //Error Logging In
                exit();
            }           
        }
        else {
            echo "ILD"; //Invalid Login Details
            exit();
        }
//When No Post of Signup or SignIn is discovered
}else {
    header("Location:". SITE_HOME);
}

2 个答案:

答案 0 :(得分:3)

你的html格式的输入ID是#username和#password。删除主题标签,它将正常工作。

答案 1 :(得分:0)

之前,你不应该使用这样的表单元素:

你必须写id =“user”而不是id =“#user”。你不能在javascript上获得这种类型的价值。 这错了:

<input id="#user" type="text" class="form-control"  placeholder="Username" name="username" />

这是正确的:

<input id="user" type="text" class="form-control"  placeholder="Username" name="username" />