如何在Ajax jQuery中正确使用return?

时间:2013-07-08 17:16:16

标签: php javascript jquery ajax

我对此登录表单有疑问。它不是让我登录到下一页。它告诉我,如果电子邮件和密码匹配,但它只是不让我登录。请看看,告诉我它有什么问题或建议我的东西将起作用。谢谢

HTML CODE

<div id="loginForm">
    <form id="formLogin" method="post" action="login.php"  >
    <input type="text" id="eMailTxt" placeholder="Email Address" />
    <input type="password" id="passWordTxt" placeholder="password" />
    <p></p>
    <input type="submit" value="Login" id="submitBtn" class="Btn" />
    </form>
</div>

jQuery AJAX CODE

    $(document).ready(function(){

        $("#formLogin").submit(function(){
        $a = $("#eMailTxt").val();
        $b = $("#passWordTxt").val();


        $.post("loginCheck.php",{
            email: $a,
            pass: $b
            },function(data){
                if (data=="false")
                {
                    $(".loginForm p").html("Password does not match").css({'color':'red'});
                    return false;
                }
                else
                {
                    $(".loginForm p").html("Password match").css({'color':'green'});
                    $("#formLogin").submit(); //I've tried return true and this submit function but none worked.
                }
            });
            return false; 
//If I turn this true, it logs in everytime even if password doesn't match. And if it's false, it will return false everytime even if password is correct
        });
    });

PHP代码

<?php
    $q=$_POST["email"];
    $s=$_POST["pass"];
    $con=mysqli_connect("localhost","root","","SocialNetwork");
    $check="SELECT PassWord FROM people WHERE EMAIL = '".$q."'";
    $data=mysqli_query($con,$check);
    $result=mysqli_fetch_array($data);
    if ($s != $result[0])
    {
        echo "false";
    }
    else
    {
        echo "true";
    }
?>

1 个答案:

答案 0 :(得分:0)

尝试这种不同的AJAX结构 - 它可能对您更有效。

此外,我将您的PHP代码更改为回显您发送的内容(作为测试,以验证PHP处理器文件是否正在接收您所期望的内容)。

从这里开始,您应该能够一次添加一个东西并使其全部工作。

<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

        <script type="text/javascript">

            $(document).ready(function() {

                $("#formLogin").submit(function(){
                    $a = $("#eMailTxt").val();
                    $b = $("#passWordTxt").val();

                    //alert($a +' '+ $b);

                    $.ajax({
                        type:"POST",
                        url: "loginCheck.php",
                        data: 'email=' +$a+ '&pass=' +$b,
                        success: function(data) {
                            alert(data);
                        }
                    });
                    return false; //This line prevents form submit -- REMOVE when done testing
                });

            }); //END $(document).ready()

        </script>
    </head>
<body>

    <div id="loginForm">
        <form id="formLogin" method="post" action="login.php"  >
        <input type="text" id="eMailTxt" placeholder="Email Address" />
        <input type="password" id="passWordTxt" placeholder="password" />
        <p></p>
        <input type="submit" value="Login" id="submitBtn" class="Btn" />
        </form>
    </div>

</body>
</html>

PHP代码: loginCheck.php

<?php
$q=$_POST["email"];
$s=$_POST["pass"];

die('Received: ['.$q.'] and ['.$s.']'); //Remove this line when done first test

$con=mysqli_connect("localhost","root","","SocialNetwork");
$check="SELECT PassWord FROM people WHERE EMAIL = '".$q."'";
$data=mysqli_query($con,$check);
$result=mysqli_fetch_array($data);
if ($s != $result[0])
{
    echo "false";
}
else
{
    echo "true";
}