【问题标题】:How to auto fill contact form 7如何自动填写联系表格 7
【发布时间】:2018-07-22 19:26:41
【问题描述】:
在我的网站上,我使用联系表格 7 将我们的价目表发送给我们的潜在客户。他们只需要填写他们的姓名和电子邮件。如果他们想预订我们的服务,客户只需单击电子邮件中的链接,即可将他们带到另一个预订表格。
我认为要求他们再次填写姓名和电子邮件看起来不太好,所以我想知道是否有办法从 cookie 或类似的东西中自动填写?
提前非常感谢
【问题讨论】:
标签:
wordpress
cookies
contact-form-7
autofill
【解决方案1】:
现在您可以简单地从“context”获取它们,因此无需插件。
您可以使用$_GET、$_POST 或post_meta 字段,如下所示:
如果您加载 example.com?your-name=John,它将从 $_GET 填充该字段
[text* your-name default:get]
或者,如果您的帖子有一个名为 your-name 的 post_meta 字段
[text* your-name default:post_meta "Your Name"]
【解决方案3】:
我会尝试用一个例子来描述这个过程。
假设表单的结构是:
<label>First Name: [text first_name]</label>
<label>Last Name: [text last_name]</label>
<label>Email: [email email]</label>
并且您希望使用作为 URL 参数接收的值预填充这些字段,例如:
http://www.your-website-domain.com/page-with-the-form/?first_name=John&last_name=Doe&email=john.doe@email.com
在表单内容的末尾输入以下代码:
<script>
var urlParams = new URLSearchParams(window.location.search);
if(urlParams.has('first_name'))
document.getElementsByName('first_name')[0].value = urlParams.get('first_name');
if(urlParams.has('last_name'))
document.getElementsByName('last_name')[0].value = urlParams.get('last_name');
if(urlParams.has('email'))
document.getElementsByName('email')[0].value = urlParams.get('email');
</script>
因此,完整的表单结构(在此假设情况下)将是:
<label>First Name: [text first_name]</label>
<label>Last Name: [text last_name]</label>
<label>Email: [email email]</label>
<script>
var urlParams = new URLSearchParams(window.location.search);
if(urlParams.has('first_name'))
document.getElementsByName('first_name')[0].value = urlParams.get('first_name');
if(urlParams.has('last_name'))
document.getElementsByName('last_name')[0].value = urlParams.get('last_name');
if(urlParams.has('email'))
document.getElementsByName('email')[0].value = urlParams.get('email');
</script>
仅此而已。