dongsui8162
2015-09-03 10:49
浏览 80

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

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:

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

图片转代码服务由CSDN问答提供 功能建议

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

表单:

 &lt; label&gt;请输入您的问题&lt; / label&gt; 
&lt; fieldset class =“question”&gt; 
 [textarea your-message id:questionmessage] 
&lt; / fieldset&gt; 
&lt; label&gt;名称&lt; / label&gt  ; 
&lt; fieldset class =“name”&gt; 
 [text * your-name id:questionname] 
&lt; / fieldset&gt; 
&lt; label&gt;电子邮件&lt; / label&gt; 
&lt; fieldset class =“email”&gt  ; 
 [email * your-email id:questionemail] 
&lt; / fieldset&gt; 
 
&lt; label&gt; [提交按钮]&lt; / label&gt; 
&lt; fieldset class =“submit”&gt; 
 [提交 “发送”] 
&lt; / fieldset&gt; 
   
 
 

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

  on_sent_ok  :“location.replace('page2');”
   
 
 

我尝试过:

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

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douchun1900 2015-09-03 15:29
    已采纳

    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

    </div>
    
    点赞 打赏 评论
  • dongsu1539 2015-09-03 11:00

    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

    点赞 打赏 评论

相关推荐 更多相似问题