0

我正在尝试使用以下 jQuery 插件(jQuery.msgBox())来执行登录到 CakePHP 站点,但我很困惑如何将数据提交到我的 php 函数以进行登录并在登录成功或显示时重定向如果不成功,则发送消息。我将不胜感激任何帮助。

这是我用来显示隐藏登录框的 jQuery 函数,当用户按下键盘上的 INSERT 键时。

jQuery(document).bind('keydown', 'insert', function(e) {
    //alert(e.keyCode);
        $.msgBox({
            type: "prompt",
            title: "Administrator Log In",
            opacity: 0.7,
            inputs: [
                { header: "User Name", type: "text", name: "username" },
                { header: "Password", type: "password", name: "password" },
                //{ header: "Remember me", type: "checkbox", name: "rememberMe", value: "theValue" }
    ],
            buttons: [
                { value: "Login" }, {value:"Cancel"}],
            success: function (result, values) {
                var v = result + " has been clicked\n";
                $(values).each(function (index, input) {
                    v += input.name + " : " + input.value + 
                    (input.checked != null ? (" - checked: " + input.checked) : "") + "\n";         
                });
                alert(v); // for testing purposes. Insert login code below
                // =======================================================

                // =======================================================
            }
        });     
});

我最终改变了 vortextangent 的答案,但这是基于他的答案。见下文.....

==================================================== ========================================

        jQuery(document).bind('keydown', 'insert', function(e) {
    //alert(e.keyCode);
        $.msgBox({
            type: "prompt",
            title: "Administrator Log In",
            opacity: 0.7,
            inputs: [
                { header: "User Name", type: "text", name: "data[User][username]" },
                { header: "Password", type: "password", name: "data[User][password]" },
                //{ header: "Remember me", type: "checkbox", name: "rememberMe", value: "theValue" }
    ],
            buttons: [
                { value: "Login" }, {value:"Cancel"}],
            success: function (result, values) {
                var v = result + " has been clicked\n";
                $(values).each(function (index, input) {
                    v += input.name + " : " + input.value + 
                    (input.checked != null ? (" - checked: " + input.checked) : "") + "\n";         
                });
                //alert(v);
        $.ajax({
        url: "/users/login",
        type: "post",
        data: values,
        success: function(responseText, statusText, xhr, $form){ // Trigger when request was successful
            //responseLogin
            if(responseText == "Success")   {
            window.location.href = '/users/account';
            }else{
            //alert("Wrong credentials. Please try again...");
            $.msgBox({
                title: "Login Error",
                content: "Wrong credentials. Please try again...",
                type: "error",
                buttons: [{ value: "Ok" }]
            });
            }
        }
        });
            }
        });     
});

==================================================== ========================================

4

1 回答 1

1

使用jQuery ajax 方法。

jQuery(document).bind('keydown', 'insert', function(e) {
//alert(e.keyCode);
    $.msgBox({
        type: "prompt",
        title: "Administrator Log In",
        opacity: 0.7,
        inputs: [
            { header: "User Name", type: "text", name: "username" },
            { header: "Password", type: "password", name: "password" },
            //{ header: "Remember me", type: "checkbox", name: "rememberMe", value: "theValue" }
],
        buttons: [
            { value: "Login" }, {value:"Cancel"}],
        success: function (result, values) {
            var v = result + " has been clicked\n";
            $(values).each(function (index, input) {
                v += input.name + " : " + input.value + 
                (input.checked != null ? (" - checked: " + input.checked) : "") + "\n";         
            });
            alert(v); // for testing purposes. Insert login code below
            // =======================================================
            $.ajax({
                url: "yourloginscript.php",
                type: "post",
                data: values,
                success: function(){ // Trigger when request was successful
                    window.location.href = 'somewhere'
                },
                error: function(request, error, errormessage) { // Show error
                    $("#error").html(error + '\n' + errormessage);
                },
                complete: otherFunction // When request is completed -no matter if the error or not
            });
            // =======================================================
        }
    });     
});
于 2013-01-02T21:56:35.967 回答