联系表格7 - 提交时设置Cookie

时间:2015-09-03 10:49:47

标签: javascript php wordpress forms cookies

所以我需要一种方法从使用联系表单7插件的表单中获取字段的值,使用管理页面上的“附加设置”框。 使用字段值设置cookie的一些方法会很棒。

表格:

<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>

其他设置,到目前为止有效:

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

我试过了:

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');"

这仍然可以正确发送电子邮件,但不会重定向到page2(我知道这应该只是将cookie值设置为1)

2 个答案:

答案 0 :(得分:0)

要正确重定向联系表单7,请添加此代码。

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

请记住以逗号开头(&#34;) - &gt;单个逗号(&#39;) - &gt;双逗号(&#34;)否则会出错

答案 1 :(得分:0)

我已拨打管理区域中的其他设置框。

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

然后在表单区域我添加了一些javascript来处理表单字段值/ cookie的东西。这可能不是很好但是有效。

< 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>

一些帮助我的链接: cookies in all pages

Cookies in javascript