how to save username and password | How to remember username and password with ajax call

In a login script, remember me feature is used to preserve the login details entered by the user. And it can be populated in the login form at the time of login. It minimizes the user effort by preventing to enter login details for each time.

Step 1 : Set id for textbox and checkbox

Step 2 : Include two jQuery files

  1. <script src=”plugins/jQuery/jQuery-2.2.0.min.js”></script>
    (Download From jQuery Website)
  2. <script src=”js/jquery.cookie.js”></script>
    (Download from https://github.com/carhartl/jquery-cookie)

Step 3 : Write down this code in login page

<script>
$(document).ready(function() {
    
        var remember = $.cookie('remember');
        if (remember == 'true') 
        {
            var email = $.cookie('email');
            var password = $.cookie('password');
            
            // autofill the fields
            $('#vEmail').val(email);
            $('#vPassword').val(password);
            $('#remember').prop("checked",true);
        }
        
});
</script>

Step 4 : Write down below code into ajax success block

 //Set cookie start
if ($('#remember').is(':checked')) {
var email = $('#vEmail').val();
var password = $('#vPassword').val();
// 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);
}
//Set cookie end

Related Posts

Divyesh Patel

I'm Divyesh Patel, Web & App developer. I want to make things that make a difference. With every line of code, i strive to make the web a beautiful place.

Leave a Reply

Your email address will not be published. Required fields are marked *