Contact Form 7 – Set Cookie on submission

So I need a way to grab the values of the fields from a form that uses contact form 7 plugin, using the Additional Settings box on the admin page.
Some way to set a cookie with the field values would be great.

The form:

Read More
<label>Please type your question</label>
<fieldset class="question">
  [textarea your-message id:questionmessage]
</fieldset>
<label>Name</label>
<fieldset class="name">
  [text* your-name id:questionname]
</fieldset>
<label>Email</label>
<fieldset class="email">
  [email* your-email id:questionemail]
</fieldset>

<label>[Submit button]</label>
<fieldset class="submit">
  [submit "Send"]
</fieldset>

Additional Settings, that works so far:

on_sent_ok: "location.replace('page2');"

I’ve tried:

on_sent_ok: "setcookie('form-email',1,strtotime('+30 days'),COOKIEPATH, COOKIE_DOMAIN,false, false);setcookie('form-name',1,strtotime('+30 days'),COOKIEPATH, COOKIE_DOMAIN,false, false);location.replace('page2');"

this still sends the email correctly, but does not redirect to page2 (I know this should just set the cookie values to 1)

Related posts

2 comments

  1. To Redirect properly contact form 7 please add this code.

    on_sent_ok: "location = 'http://example.com/';"
    

    Remember start double commas(“) -> single commas(‘) -> double commas(“) otherwise there will be error

  2. I’ve called in the additional settings box in the admin area.

    on_sent_ok: "setCookiesAndRedirect("http://www.redirecthere.com");"
    

    Then in the form area I’ve added some javascript to handle the form field values/cookie stuff. This probably isn’t great but it works.

    < script type = "text/javascript" >
      window.onload = function() {
        var namefield = document.getElementById("formfullname");
        var emailfield = document.getElementById("formemail");
        var firstnamefield = document.getElementById("formfirstname");
        var surnamefield = document.getElementById("formsurname");
        var namecookie = getCookie("form-fullname");
        var emailcookie = getCookie("form-email");
        var firstnamecookie = getCookie("form-firstname");
        var surnamecookie = getCookie("form-surname");
        if (namecookie != "" && namefield != null) {
          namefield.value = namecookie;
        }
        if (emailcookie != "" && emailfield != null) {
          emailfield.value = emailcookie;
        }
        if (firstnamecookie != "" && firstnamefield != null) {
          firstnamefield.value = firstnamecookie;
        }
        if (surnamecookie != "" && surnamefield != null) {
          surnamefield.value = surnamecookie;
        }
      } //end load function
    
    function setCookiesAndRedirect(url) {
        var namefield = document.getElementById("formfullname");
        var emailfield = document.getElementById("formemail");
        var firstnamefield = document.getElementById("formfirstname");
        var surnamefield = document.getElementById("formsurname");
        if (namefield != null)
          setCookie("form-fullname", namefield.value, 30);
        if (emailfield != null)
          setCookie("form-email", emailfield.value, 30);
        if (firstnamefield != null)
          setCookie("form-firstname", firstnamefield.value, 30);
        if (surnamefield != null)
          setCookie("form-surname", surnamefield.value, 30);
        location.replace(url);
      } //end setCookiesAndRedirect
    
    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=/";
      } //end setCookies
    
    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 "";
    } </script>
    <p>First Name (required)<br />
       [text* Firstname id:formfirstname] </p>
    
    <p>Last Name (required)<br />
       [text* LastName id:formsurname] </p>
    
    <p>Your Email (required)<br />
        [email* your-email id:formemail] </p>
    
    <p>[submit "Download Now"]</p>

    Some links that helped me:
    cookies in all pages

    Cookies in javascript

Comments are closed.