简体   繁体   English

jQuery AJAX PHP 登录表单

[英]jQuery AJAX PHP login form

I am trying to make a login form for a webapp.我正在尝试为 webapp 制作登录表单。

My login form我的登录表格

<form class="col s12" method="post" action="#">
    <div class="row">
        <div class="input-field col s12">
            <input id="username" type="text" name="name" required>
            <label for="username">Username</label>
        </div>
    </div>
        <div class="row">
            <div class="input-field col s12">
                 <input id="password" type="password" name="pas" required>
                 <label for="password">Password</label>
            </div>
        </div>
    </div>
    <div class="row">
        <div id="error"></div>
        <div class="card-action">
            <button class="btn waves-effect waves-light" id="loginBtn" type="submit" value="Log In">Submit
                <i class="mdi-content-send right"></i>
            </button>
        </div>
    </div>
</form>

Button click script按钮点击脚本

<script>
    $(document).ready(function() {

        $('#loginBtn').click(function() {

            var username=$("#username").val();
            var password=$("#password").val();
            var dataString = 'username='+username+'&password='+password;
            if($.trim(username).length>0 && $.trim(password).length>0) {

                $.ajax({
                    type: "POST",
                    url: "login.php",
                    data: dataString,
                    cache: false,
                    beforeSend: function(){ $("#loginBtn").val('Connecting...');},
                    success: function(data){
                        if(data) {
                            window.location.href = "application.php";
                        } else {
                            $('#loginCard').shake();    //Shake animation effect.
                            $("#error").html("<span style='color:#cc0000'>Error:</span> Invalid username and password. ");
                        }
                    }

                });
            }
            return false;

        });

    });
</script>

login.php登录.php

<?php
    include("db.php");
    session_start();
    if(isset($_POST['username']) && isset($_POST['password'])) {

        $username=mysqli_real_escape_string($db,$_POST['username']);
        $password=msqli_real_escape_string($db,$_POST['password']);
        $result=mysqli_query($db,"SELECT uname FROM users WHERE uname='$username' and pass='$password'");
        $count=msqli_num_rows($result);
        $row=mysqli_fetch_array($result,MYSQLI_ASSOC);

        if($count==1) {

            $_SESSION['login_user']=$row['uname'];
            echo $row['uname']

        }

    }
?>

Whenever I click the submit button it redirects to the same page (index.php) by POST method.每当我单击提交按钮时,它都会通过 POST 方法重定向到同一页面(index.php)。 In the chrome developer tools, the submit data never gets sent to login.php, and I am not able to understand what's causing this issue.在 chrome 开发人员工具中,提交数据永远不会发送到 login.php,我无法理解导致此问题的原因。 Any help would be appreciated.任何帮助,将不胜感激。

You have to change你必须改变

<form class="col s12" method="post" action="#">

to

<form id="loginForm" class="col s12" method="post" action="#">

and instead of:而不是:

$('#loginBtn').click(function() {

you should have:你应该有:

$('#loginForm').submit(function(e){
    e.preventDefault();
    // Rest of the code here
});

or if the above does not work, you can try this one:或者如果上述方法不起作用,您可以试试这个:

$('#loginForm').submit(function(e){
    // call a function for the ajax job
    return false;
});
 <script>
    $(document).ready(function () {
        $("#login").submit(function (e)
        {   

            var firstForm = $("#login").serialize();

            $.ajax({
                url: "login.php",
                type: 'POST',
                data: firstForm,
                mimeType: "multipart/form-data",
                //contentType: false,
                cache: false,
                processData: false,
                success: function (data)
                {                     
                    if ($.trim(data) === '1') {
                       window.location.href = 'display.php';                                 
                    }
                    else {
                        $('#msgbox').html('Invalid Email id or password.');
                        $('#msgbox').addClass("success").css("color", "red").fadeIn();
                        $('#msgbox').addClass("success").css("font-size", "15px");
                    }
                }
            });
            e.preventDefault();
        });

    });
</script>

I am inclined to think that the problem occurs because, by default, the call through ajax is xxxx (which means that the rest of the process continues and does not wait for the server's response).我倾向于认为问题的出现是因为,默认情况下,通过 ajax 的调用是 xxxx (这意味着该过程的其余部分继续进行,并且不等待服务器的响应)。 You should use: 'async: false' as an additional parameter in the call to ajax::您应该在调用 ajax:: 时使用:'async: false' 作为附加参数

$.ajax({
            url: "login.php",
            type: 'POST',
            data: firstForm,
            mimeType: "multipart/form-data",
            //contentType: false,
            async: false,
            cache: false,
            processData: false,
            success: function (data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM