简体   繁体   English

使用JQuery ajax和php登录表单

[英]Login form with JQuery ajax and php

The Goal: 目标:
Step 1: A form opens in a JQuery Dialog. 第1步:在JQuery对话框中打开一个表单。 User Enters the username and Password, Clicks on Login. 用户输入用户名和密码,点击登录。 Step 2: Ajax Sends the data to a server side script , in my case loginproc.php. 第2步: Ajax将数据发送到服务器端脚本,在我的例子中是loginproc.php。
Step 3: Validate the login using loginproc. 第3步:使用loginproc验证登录。 If the login is successful the user is redireted to a page. 如果登录成功,则用户将重定向到页面。
Step 4: Else a message is send back to the dialog and it is appended in the dialog itself along with a shake effect. 步骤4:否则将消息发送回对话框,并将其附加在对话框本身以及抖动效果中。

As of now step one and three works fine if I use '$("#admin-form").sumbit();' 截至目前,如果我使用'$(“#admin-form”),第一步和第三步工作正常.sumbit();' just before closing the dialog. 就在关闭对话框之前。 I am not sure as to how can I set the invalid login text in the diolog. 我不知道如何在diolog中设置无效的登录文本。

Code: 码:

$(function() {
    var name = $("#name"),
            password = $("#password"),
            allFields = $([]).add(name).add(password),
            tips = $(".validateTips");

    function updateTips(t) {
        tips
                .text(t)
                .addClass("ui-state-highlight");
        setTimeout(function() {
            tips.removeClass("ui-state-highlight", 1500);
        }, 500);
    }

    function checkLength(o, n, min, max) {
        if (o.val().length > max || o.val().length < min) {
            o.addClass("ui-state-error");
            updateTips("Length of " + n + " must be between " +
                    min + " and " + max + ".");
            return false;
        } else {
            return true;
        }
    }

    function checkRegexp(o, regexp, n) {
        if (!(regexp.test(o.val()))) {
            o.addClass("ui-state-error");
            updateTips(n);
            return false;
        } else {
            return true;
        }
    }

    $("#login-form").dialog({
        autoOpen: false,
        /*      height: 300, */
        width: 450,
        show: {
            effect: "explode",
            duration: 1000
        },
        hide: {
            effect: "explode",
            duration: 1000
        },
        modal: true,
        buttons: {
            "Login": function() {
                var bValid = true;
                allFields.removeClass("ui-state-error");

                bValid = bValid && checkLength(name, "username", 3, 16);
                bValid = bValid && checkLength(password, "password", 5, 32);
                bValid = bValid && checkRegexp(name, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter.");
                bValid = bValid && checkRegexp(password, /^([0-9a-zA-Z_])+$/, "Password field only allow : a-z 0-9");
                if (bValid) {
                    $("#admin_login").submit();
                    $(this).dialog("close");
                }
            },
            Cancel: function() {
                $(this).dialog("close");
            }
        },
        close: function() {
            allFields.val("").removeClass("ui-state-error");
        }
    });

    // Link to open the dialog
    $("#admin-link").click(function(event) {
        $("#login-form").dialog("open");
        event.preventDefault();
    });

});

This is the section of the code in which I need to make some changes. 这是我需要进行一些更改的代码部分。

if (bValid) {
     $("#admin_login").submit();
     $(this).dialog("close");
}

Kindly help me figure this out 请帮我解决这个问题

UPDATE: The Form: 更新:表格:

<form method="post" action="class/loginproc.php" id="admin_login">
<fieldset>
 <label for="name">Username</label>
<input type="text" name="user" id="name" class="text ui-widget-content ui-corner-all" />
 <label for="password">Password</label>
<input type="password" name="pass" id="password" value="" class="text ui-widget-content ui-corner-all" />
</fieldset>
</form>

Loginproc.php Loginproc.php

<?php

require_once 'utils.php';
if (is_null($_POST['user']) || is_null($_POST['pass'])) {
    utils::redirect_url("../index.php");
} else {
    $utils = new utils();
    $username = $_POST['user'];
    $password = $_POST['pass'];
    $type = 'admin';
    $response = $utils->validate_user($username, $password, $type);
    if ($response == true) {
        return true;
        /*
          $utils->login_redirect($type); */
    } else {
        return false;
        /* $_SESSION['valid'] = false;
          utils::redirect_url("../index.php"); */
    }
}
?>

The Jquery: Jquery:

 if (bValid) {
 var data = $('#admin_login').serialize();
 $.ajax({
 url: "class/loginproc.php",
 type: "post",
 data: data,
 dataType: "json",
 success: function(data) {
 if (data.success) {
   window.location = "../admin.php";
 }
 else {
   alert('Invalid Login');
  }
 }
                });
                $(this).dialog("close");
            }

Solution: Thanks to 'Mohammad Adil' , I was able to figure this out. 解决方案:感谢'Mohammad Adil',我能够解决这个问题。 I had to change his provided solution a bit to get the appropriate result. 我不得不改变他提供的解决方案以获得适当的结果。 Thus I am posting it here for future reference. 因此,我将其发布在此处以供将来参考。
JQUERY JQUERY

  if (bValid) {
     var data = $('#admin_login').serialize();
     $.ajax({
     url: "class/loginproc.php",
     type: "post",
     data: data,
     dataType: "json",
     success: function(data) {
      window.location = "admin.php";
     },
     error: function(data) {
       alert('invalid');
     }
   });
   $(this).dialog("close");
  }

Loginproc: Loginproc:

  $response = $utils->validate_user($username, $password, $type);
    if ($response == true) {
        echo true;
    } else {
        echo false;
    }

Regards 问候
Genocide_Hoax Genocide_Hoax

 if (bValid) {
    var data = $('#admin_login').serialize();                  
          $.ajax({
              url:"loginproc.php",
              type: "post",
              data : data,
              dataType:"text",
              success:function(data){
                  if(data == "true"){ 
                    window.location = "some location";
                  }
                 else{
                       // LOGIN FAILED
                 }
              }
           });
         $(this).dialog("close");
 }

你把方法和操作放在表单中,我认为如果使用Ajax应该省略它,因为如果不省略,数据将不会异步发送。

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

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