Ajax响应消息不起作用

时间:2016-03-31 00:28:48

标签: javascript php jquery ajax

所以我有这个脚本,我在我的网站上运行,它是一个密码重置系统。当我运行脚本时,错误消息不显示在这里是我的ajax脚本

这是我的ajax脚本

$(document).ready(function() 
{
    $('#doforgot').click(function()
    {
        var login=$("#login").val();
        var dataString = 'forLogin='+login;
        $.ajax({
            type: "POST",
            url: "Application/Run/Other/Forgot.php",
            data: dataString,
            cache: false,
            beforeSend: function(){ $("#doforgot").val('Connecting...'); },
            success: function(data){
                if (data === "3")
                {
                    $("#doforgot").val('Reset your Account!');
                    $("#formsg").html('<div class="alert alert-success-light-text fade in"><button type="button" class="close" data-dismiss="alert" aria-hidden="true"><i class="fa fa-times"></i></button><p><i class="fa fa-trophy"></i> <strong>Success!</strong> <a href="help/alerts" class="alert-link">We have just an email with a link to reset your password!</a>. Click on it and type a new password and hit change, then just login with your new password.</p><p><a href="help/report" class="btn btn-meadow">Report a glitch</a></p></div>');
                }
                else if (data === "4")
                {
                    $("#doforgot").val('Reset your Account!');
                    $("#formsg").html('<div class="alert alert-hearts-light fade in"><button type="button" class="close" data-dismiss="alert" aria-hidden="true"><i class="fa fa-times"></i></button><p><strong>Forgot Failed</strong> · <a href="#" class="alert-link">The account does not exists</a>. Change a few things and try submitting again.</p><p><a href="help/report" class="btn btn-berry">Report a glitch</a></p></div>');
                }
                else if (data === "1")
                {
                    $("#doforgot").val('Reset your Account!');
                    $("#formsg").html('<div class="alert alert-hearts-light fade in"><button type="button" class="close" data-dismiss="alert" aria-hidden="true"><i class="fa fa-times"></i></button><p><strong>Forgot Failed</strong> · <a href="#" class="alert-link">Please fill in all the required fields</a>. Change a few things and try submitting again.</p><p><a href="help/report" class="btn btn-berry">Report a glitch</a></p></div>');
                }
                else
                {
                    $("#doforgot").val('Reset your Account!');
                }

            }
        });
        return false;
    });
});

这是我的php脚本

include '../global.php';
    if (isset($_POST['forLogin'])) {
        if (empty($_POST["forLogin"])) {
            echo "1";
        } else {
            $login = $_POST["forLogin"];
            if (forgot($login, $dblink) == true) {
                echo "3";
            } else {
                $response = forgot($login, $dblink);
                echo $response;
            }
        }
    } else {
        echo "1";
    }

这是函数

function checkIfUsernameExists($text, $link) {
    if ($stmt = $link->prepare("SELECT * FROM users WHERE username = ? LIMIT 1")) {
        $stmt->bind_param('s', $text);
        $stmt->execute();
        $stmt->store_result();
        if ($stmt->num_rows == 1) {
            return true;
        }
    }
}
function checkIfEmailExists($text, $link) {
    if ($stmt = $link->prepare("SELECT * FROM users WHERE email = ? LIMIT 1")) {
        $stmt->bind_param('s', $text);
        $stmt->execute();
        $stmt->store_result();
        if ($stmt->num_rows == 1) {
            return true;
        }
    }
}
function forgot($login, $dblink) {
    if (checkIfUsernameExists($login) == true) {
        if ($stmt = $dblink->prepare("SELECT id, username, email, password, name FROM users WHERE username = ? LIMIT 1")) {
            $stmt->bind_param('s', $login);
            $stmt->execute();
            $stmt->store_result();
            $stmt->bind_result($forUserID, $forUsername, $forEmail, $forPassword, $forName);
            $stmt->fetch();
            if ($stmt->num_rows == 1) {
                mailResetLink($forEmail, $forUsername, $forName);
                return true;
            }
        }
    } elseif (checkIfEmailExists($login) == true) {
        if ($stmt = $dblink->prepare("SELECT id, username, email, password, name FROM users WHERE email = ? LIMIT 1")) {
            $stmt->bind_param('s', $login);
            $stmt->execute();
            $stmt->store_result();
            $stmt->bind_result($forUserID, $forUsername, $forEmail, $forPassword, $forName);
            $stmt->fetch();
            if ($stmt->num_rows == 1) {
                mailResetLink($forEmail, $forUsername, $forName);
                return true;
            }
        }
    } else {
        echo "4";
    }
}

最后这是我的html脚本

<!DOCTYPE html>
<html>
<head>
    <title>Ajax Test</title>

</head>
<body>
<form action="" method="post">
Username or Email
<input type="text" class="input"  id="login">
<input type="submit" class="button button-primary" value="Reset your Account!" id="doforgot">
<div id="formsg"></div>
</div>
</form> 

</body>
</html>

由于某种原因,它会重新加载页面而不应该。

1 个答案:

答案 0 :(得分:0)

我尝试了你的代码,经过一些调整后工作正常。这都是你的HTML和JavaScript吗?

你有运行的php服务器吗?你的ajax调用中的URL应该是一个url

我只是将javascript合并到html文件中。因为我很懒,只需将索引文件放在与php相同的目录中。确保你的php服务器正在运行。

的index.html

<!DOCTYPE html>
<html>
<head>
    <title>Ajax Test</title>
    <script   src="https://code.jquery.com/jquery-2.2.2.min.js" integrity="sha256-36cp2Co+/62rEAAYHLmRCPIych47CvdM+uTBJwSzWjI="   crossorigin="anonymous"></script>
    <script>
        $(document).ready(function() 
            {
                alert('asdf');

                $('#doforgot').click(function()
                {
                    var login=$("#login").val();
                    var dataString = 'forLogin='+login;
                    $.ajax({
                        type: "POST",
                        url: "inv.php",
                        data: dataString,
                        cache: false,
                        beforeSend: function(){ $("#doforgot").val('Connecting...'); },
                        success: function(data){
                            if (data === "3")
                            {
                                $("#doforgot").val('Reset your Account!');
                                $("#formsg").html('<div class="alert alert-success-light-text fade in"><button type="button" class="close" data-dismiss="alert" aria-hidden="true"><i class="fa fa-times"></i></button><p><i class="fa fa-trophy"></i> <strong>Success!</strong> <a href="help/alerts" class="alert-link">We have just an email with a link to reset your password!</a>. Click on it and type a new password and hit change, then just login with your new password.</p><p><a href="help/report" class="btn btn-meadow">Report a glitch</a></p></div>');
                            }
                            else if (data === "4")
                            {
                                $("#doforgot").val('Reset your Account!');
                                $("#formsg").html('<div class="alert alert-hearts-light fade in"><button type="button" class="close" data-dismiss="alert" aria-hidden="true"><i class="fa fa-times"></i></button><p><strong>Forgot Failed</strong> · <a href="#" class="alert-link">The account does not exists</a>. Change a few things and try submitting again.</p><p><a href="help/report" class="btn btn-berry">Report a glitch</a></p></div>');
                            }
                            else if (data === "1")
                            {
                                $("#doforgot").val('Reset your Account!');
                                $("#formsg").html('<div class="alert alert-hearts-light fade in"><button type="button" class="close" data-dismiss="alert" aria-hidden="true"><i class="fa fa-times"></i></button><p><strong>Forgot Failed</strong> · <a href="#" class="alert-link">Please fill in all the required fields</a>. Change a few things and try submitting again.</p><p><a href="help/report" class="btn btn-berry">Report a glitch</a></p></div>');
                            }
                            else
                            {
                                $("#doforgot").val('Reset your Account!');
                            }

                        }
                    });
                    return false;
                });
            });
    </script>
</head>
<body>
<form action="" method="post">
Username or Email
<input type="text" class="input"  id="login">
<input type="submit" class="button button-primary" value="Reset your Account!" id="doforgot">
<div id="formsg"></div>
</div>
</form> 

</body>
</html>