简体   繁体   English

提交由preventDefalut()阻止

[英]submit prevented by preventDefalut()

I am using ajax to make validation of login. 我正在使用ajax来验证登录。 I am having a problem with javascript e.preventeDefault(), when use it prevents submit and always shows validation empty field. 我遇到了javascript e.preventeDefault()的问题,当使用它时会阻止提交并始终显示验证空字段。 When remove it is right but echo json data and doesnt shows validation messages inside dialog box, instead it redirects to url and echo the right json message. 当删除它是正确的但回显json数据并且不显示对话框内的验证消息,而是重定向到url并回显正确的json消息。 I think e.preventDefault prevents submit, is there any other way to put validation message inside dialog box insted of e.preventDefault() ? 我认为e.preventDefault会阻止提交,有没有其他方法可以在e.preventDefault()的对话框中放置验证消息?

$('#login_form').on('submit', function(e) {
    e.preventDefault(); 
    var username = $('#login_form input[name=sign_in_username]').val();
    var password = $('#login_form input[name=sign_in_pass]').val();
        $.ajax({
          url: "login.php",
          type: "POST",
          data: {username: username,
                 password: password
                },
          dataType: 'json',
          success: function(response) {

            if(response.status){ 
                           $(this).unbind('submit').submit()
            console.log(response);
            window.location = response.url;
               }
               else{
                   $('#invalid_content').html(response.msg);                                 
                }
            }
        });               
    });

login.php 的login.php

if (((!isset($_POST["sign_in_pass"]) || !isset($_POST["sign_in_username"]) ) || trim($_POST["sign_in_pass"]) == "" || trim($_POST["sign_in_username"]) == "")) {
    echo json_encode(array('msg' => "Username or password are empty.", 'url' => "", 'status' => false));
    exit();
}
$rows = query("SELECT * FROM customers WHERE username = ?", $_POST["sign_in_username"]);

// nese form eshte bere submit
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (count($rows) == 1) {
        $row = $rows[0];
        if (crypt($_POST["sign_in_pass"], $row["hash"]) == $row["hash"]) {
            $_SESSION["id"] = $row["id"];
            echo    json_encode(array('msg' => "Success.", 'url' => "index.php", 'status' => true));
        }
    } else {
        echo json_encode(array('msg' => "Username  or password invalid.", 'url' => "", 'status' => false));
    }
} else {
    echo json_encode(array('msg' => "Username  or password invalid.", 'url' => "", 'status' => false));
}

Your problem is not the e.preventDefault() . 你的问题不是e.preventDefault() Your fields just don't match up, which is why they're empty. 你的字段不匹配,这就是为什么它们是空的。

Your data parameter should be: data: {sign_in_username: username, sign_in_password: password} . 您的data参数应为: data: {sign_in_username: username, sign_in_password: password}

The request your PHP script receives has the wrong field names in it. 您的PHP脚本收到的请求中包含错误的字段名称。

In the future, for debugging purposes, on your PHP script, try var_dump($_POST); 将来,出于调试目的,在PHP脚本上尝试var_dump($_POST); . This will give you an idea of what the request you received had in it. 这将让您了解您收到的请求中包含的内容。

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

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