2

I'm working on a project that I set functions to create a cookie based off of a form value. That cookie then populates another form's input values. At first, I only needed to set three cookies, so this wasn't overly big (calling the functions)... the requirements changed, and I went from needing three cookies to now needing eight, so my function calls are getting a little out of hand. Is there a more efficient way of doing this?

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

function checkCookie(c, el) {
    var user = getCookie(c);
    if (user != "") {
        document.getElementById(el).value = user;
        document.getElementById(el).setAttribute("value", user);
        document.getElementById(el).setAttribute("readonly", "readonly");
    }
} 

function setEmployee(e, field) {
    var emp = getCookie(e);
    if (emp != "") {
        document.getElementsByClassName(field).value = emp;
        var perm = document.getElementsByClassName(field);
        perm[0].setAttribute("value", emp);

    }
}


$(document).ready(function(){
    var firstName = "firstName";
    var lastName = "lastName";
    var phoneCookie = "phone";
    var emailCookie = "email";
    var companyCookie = "company";
    var addressCookie = "address";
    var stateCookie = "state";
    var zipCookie = "zip";

    $('#form').submit(function(){
        var first = $('#FirstName').val();
        var last = $('#LastName').val();
        var phone = $('#Phone').val();
        var email = $('#Email').val();
        var company = $('#Company').val();
        var address = $('#Address').val();
        var state = $('#State').val();
        var zip = $('#PostalCode').val();

        setCookie(firstName, first, 10);
        setCookie(lastName, last, 10);
        setCookie(phoneCookie, phone, 10);
        setCookie(emailCookie, email, 10);
        setCookie(companyCookie, company, 10);
        setCookie(addressCookie, address, 10);
        setCookie(stateCookie, state, 10);
        setCookie(zipCookie, zip, 10);
    });

    if($('#census').is(':visible')){
        checkCookie(firstName, "contactName");
        checkCookie(lastName, "contactLast");
        checkCookie(phoneCookie, "contactPhone");
        checkCookie(emailCookie, "contactEmail");
        checkCookie(companyCookie, "contactCompany");
        checkCookie(addressCookie, "contactAddress");
        checkCookie(stateCookie, "contactState");
        checkCookie(zipCookie, "contactZip");
    }
});
  • Ideally cookie should only act a session id and then you recognize your client based on that. In your case you are using cookies to store data instead of an identifier to that data. I would recommend setting one id in you r case employee id and retrieve/render form based on that id. – Adi Oct 12 '17 at 03:14
  • I'm voting to close this question as off-topic because it belongs on http://codereview.stackexchange.com – user229044 Oct 12 '17 at 03:27
  • @meager - I had no clue that was a thing. I'll move it over to there. – Brittany Layne Rapheal Oct 12 '17 at 03:28
  • @Adi - Two questions: 1. Would that require a database? (This website doesn't have a database.) 2. If not, is it possible to save the session id for a set amount of days? The reason I went the cookie route is because the information will need to be stored for 10 days in case the user needs to come back to the second half of the form at a later date. – Brittany Layne Rapheal Oct 12 '17 at 03:45

0 Answers0