• KA-Creative

    (@ka-creative)


    Hi, I am trying to add cookies to my pop-up forms at http://www.pharmasystemsusa.com but am having trouble getting it to work. I am very new to javascript, so I’m not sure if I’m doing this right or if I’m even close.

    The website has several downloads that the client would like to collect contact information on, so every download has a pop-up form that requires the viewer to complete in order to receive the download. To improve the user experience, the client would like for the contact form to remember the viewer’s information so that they only have to enter it once.

    After some online research, I’ve applied the following code to my pop-up. Anyone have any ideas what I’m doing wrong here?

    This particular pop-up can be found by clicking on the Rota Brochure on this page: http://pharmasystemsusa.com/pharma-equipment/select-by-machine-type/fill-finish-lines/rota-rotary-washers/

    ‘<h1>DOWNLOAD ACCESS</h1>
    Please complete the form below to access the ROTA Brochure:

    <form action=”http://pharmasystemsusa.com/wp-content/uploads/2016/05/Rota-Brochure.pdf&#8221; method=”POST”>
    <script type=”text/javascript”>var submitted=false;</script>
    <iframe name=”hidden_iframe” id=”hidden_iframe” style=”display:none;”
    onload=”if(submitted) {window.location=’http://pharmasystemsusa.com/wp-content/uploads/2016/05/Rota-Brochure.pdf&#8217;;}”></iframe>
    <form action=”http://pharmasystemsusa.com/wp-content/uploads/2016/05/Rota-Brochure.pdf&#8221; method=”post” target=”hidden_iframe”
    onsubmit=”submitted=true;”>

    [contact-form to='myemail' subject='PharmaSystems Downloads - New Contact']
    [contact-field label='First Name' type='name' name="usernameF" id="usernameF" required='1'/]
     [contact-field label='Last Name' type='text' name="usernameL" id="usernameL" required='1'/]
     [contact-field label='Company' type='text' name="company" id="company" required='1'/]
     [contact-field label='State / Province / Region' type='text' name="location" id="location" required='1'/]
     [contact-field label='Country' type='text' name="country" id="country" required='1'/]
     [contact-field label='Inquiry Status' type='select' required='1' options='Active Project,Just Browsing'/]
     [contact-field label='Email' type='email' name="email" id="email" required='1'/]
     [contact-field label='Phone' type='text' name="phone" id="phone" required='1'/][/contact-form]

    <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 = firstname + “=” + value + “; ” + expires;
    document.cookie = lastname + “=” + value + “; ” + expires;
    document.cookie = company + “=” + value + “; ” + expires;
    document.cookie = location + “=” + value + “; ” + expires;
    document.cookie = country + “=” + value + “; ” + expires;
    document.cookie = email + “=” + value + “; ” + expires;
    document.cookie = phone + “=” + value + “; ” + expires;
    }

    function getCookie(cname) {
    var name = firstname + “=”;
    var name = lastname + “=”;
    var name = company + “=”;
    var name = location + “=”;
    var name = country + “=”;
    var name = email + “=”;
    var name = phone + “=”;
    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() {
    var user = getCookie(“firstname”);
    var user = getCookie(“lastname”);
    var user = getCookie(“company”);
    var user = getCookie(“location”);
    var user = getCookie(“country”);
    var user = getCookie(“email”);
    var user = getCookie(“phone”);
    if (user != “”) {
    alert(“Welcome again ” + user);
    } else {
    user = prompt(“Please enter your name:”, “”);
    if (user != “” && user != null) {
    setCookie(“username”, user, 365);
    }
    }
    }

    >

Viewing 2 replies - 1 through 2 (of 2 total)
  • ancawonka

    (@ancawonka)

    Well, I came here to read and ask about cookies, too. I don’t have the exact answer for you, but here are some things for you to try (if you haven’t already).

    0) Check to make sure that the contact form shortcode doesn’t generate a totally different form than the one you’re trying to post. Depending on which form tool you’re using, there may already be a different JavaScript event that gets triggered on clicking the submit button.

    1) Confirm that the cookie is set correctly. Using your browser’s developer tools, you can view the cookies that have been set for that page (and their values). For example, in Safari, I can see cookies under the “Storage” tab of the developer tools. In Chrome, they are under “Resources->Cookies”.

    2) Note that if you use document.cookie, you are ONLY setting a cookie for that particular URL – so, if you have another page on the same domain you won’t see the cookie. Take a look at this: https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie

    3) you might also consider putting all of the values in a single cookie (instead of creating a cookie for each field value).

    Thread Starter KA-Creative

    (@ka-creative)

    Thanks ancawonka! Your note on #2 is particularly very helpful since I need the cookies to work across the site rather than just on 1 URL. If I do find any solutions, I’ll post them here in case it’s along the lines of what you’re also looking for.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Javascript for adding Cookies to a Contact Form’ is closed to new replies.