-2

我在使用此登录表单时遇到问题。它不允许我登录到下一页。它会告诉我电子邮件和密码是否匹配,但它只是不允许我登录。请看一下并告诉我它有什么问题或建议我一些可行的方法。谢谢

代码

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

    $(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";
    }
?>
4

1 回答 1

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";
}
于 2013-07-08T17:33:32.067 回答