MTA & HTML Login & Remember Me

1.2k views Asked by At

I'm trying to solve one HTML input bar so I can remember the MTA server username and password Login is available here: http://ru1n.eu/index.php?/topic/3-rel-login-panel/ and I found some javascript for remembering and i do not know how to make a login to incorporate it

<script type="text/javascript">

    if ($('#remember').attr('checked')) 
    {
        var email = $('#email').attr("value");
        var password = $('#password').attr("value");

        // set cookies to expire in 14 days
        $.cookie('email', email, { expires: 14 });
        $.cookie('password', password, { expires: 14 });
        $.cookie('remember', true, { expires: 14 });                
    }
    else
    {
        // reset cookies
        $.cookie('email', null);
        $.cookie('password', null);
        $.cookie('remember', null);
    }

    var remember = $.cookie('remember');
    if (remember == 'true') 
    {
        var email = $.cookie('email');
        var password = $.cookie('password');
        // autofill the fields
        $('#email').attr("value", email);
        $('#password').attr("value", password);
    }

</script>  
3

There are 3 answers

3
arbuthnott On

You have two blocks of code in the same place that should be running in different places. The second block, that checks for cookies and fills the html form - that should be done when the page is finished loading. It looks like you are using jquery, so that might look like this:

<script type="text/javascript">
    $(function() {
        var remember = $.cookie('remember');
        if (remember == 'true') 
        {
            var email = $.cookie('email');
            var password = $.cookie('password');
            // autofill the fields
            $('#email').attr("value", email);
            $('#password').attr("value", password);
        }
    });
</script>

The other block that sets the cookies should be run when the user submits the form. For example in some function:

function submitForm() {
    if ($('#remember').attr('checked')) 
    {
        var email = $('#email').attr("value");
        var password = $('#password').attr("value");

        // set cookies to expire in 14 days
        $.cookie('email', email, { expires: 14 });
        $.cookie('password', password, { expires: 14 });
        $.cookie('remember', true, { expires: 14 });                
    }
    else
    {
        // reset cookies
        $.cookie('email', null);
        $.cookie('password', null);
        $.cookie('remember', null);
    }

    // now continue with form submission...
}
0
Ru1n On

Ok, So will the code look like this? https://ctrlv.cz/XeO3

and this code -var email = $ ('# email') is what's in the username in the name = "" or id = ""

0
Ru1n On

would such a script not be sent and used?

$(function () {

if (localStorage.chkbox && localStorage.chkbox != '') {
    $('#rememberChkBox').attr('checked', 'checked');
    $('#signinId').val(localStorage.username);
    $('#signinPwd').val(localStorage.pass);
} else {
    $('#rememberChkBox').removeAttr('checked');
    $('#signinId').val('');
    $('#signinPwd').val('');
}

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

    if ($('#rememberChkBox').is(':checked')) {
        // save username and password
        localStorage.username = $('#signinId').val();
        localStorage.pass = $('#signinPwd').val();
        localStorage.chkbox = $('#rememberChkBox').val();
    } else {
        localStorage.username = '';
        localStorage.pass = '';
        localStorage.chkbox = '';
    }
});

});