0

次の jQuery プラグイン ( jQuery.msgBox() ) を使用して CakePHP サイトへのログインを実行しようとしていますが、ログインのためにデータを php 関数に送信し、ログインが成功した場合はリダイレクトする方法について混乱しています。失敗した場合のメッセージ。助けていただければ幸いです。

これは、ユーザーがキーボードの INSERT キーを押したときに、非表示のログイン ボックスを表示するために使用している jQuery 関数です。

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 に答える