联系表格 7 防止重复字段值提交
Posted
技术标签:
【中文标题】联系表格 7 防止重复字段值提交【英文标题】:Contactform7 prevent duplicate field value submission 【发布时间】:2014-06-27 03:58:52 【问题描述】:我正在使用 WordPress 3.8 和带有联系表格 7 db 扩展名的联系表格 7 插件。
我想检查我在functions.php中的钩子(alter_wpcf7_posted_data)上提交的现有电子邮件,如下所示:
function alter_wpcf7_posted_data( $data )
global $wpcf7;
if(email_exists( $_POST['mail']))
$data = array();
return $data;
add_filter("wpcf7_posted_data", "alter_wpcf7_posted_data");
这个钩子在源上引发了一个错误,但不保存数据。
基本上,如果 email_exists() 返回 true,我不希望保存数据并在表单上抛出验证错误。
有谁知道如何防止表单提交。
注意:我没有使用 AJAX 表单提交。
【问题讨论】:
你找到解决方案了吗? 对不起,伙计们,现在还没有在 WP 上工作。所以,我会把这个留给那些支持答案的人。 【参考方案1】:我找到了解决方案。只需将此代码添加到您的 function.php 中
/**
* @param $formName string
* @param $fieldName string
* @param $fieldValue string
* @return bool
*/
function is_already_submitted($formName, $fieldName, $fieldValue)
require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php');
$exp = new CFDBFormIterator();
$atts = array();
$atts['show'] = $fieldName;
$atts['filter'] = "$fieldName=$fieldValue";
$atts['unbuffered'] = 'true';
$exp->export($formName, $atts);
$found = false;
while ($row = $exp->nextRow())
$found = true;
return $found;
/**
* @param $result WPCF7_Validation
* @param $tag array
* @return WPCF7_Validation
*/
function my_validate_email($result, $tag)
$formName = 'email_form'; // Change to name of the form containing this field
$fieldName = 'email_123'; // Change to your form's unique field name
$errorMessage = 'Email has already been submitted'; // Change to your error message
$name = $tag['name'];
if ($name == $fieldName)
if (is_already_submitted($formName, $fieldName, $_POST[$name]))
$result->invalidate($tag, $errorMessage);
return $result;
// use the next line if your field is a **required email** field on your form
add_filter('wpcf7_validate_email*', 'my_validate_email', 10, 2);
// use the next line if your field is an **email** field not required on your form
add_filter('wpcf7_validate_email', 'my_validate_email', 10, 2);
// use the next line if your field is a **required text** field
add_filter('wpcf7_validate_text*', 'my_validate_email', 10, 2);
// use the next line if your field is a **text** field field not required on your form
add_filter('wpcf7_validate_text', 'my_validate_email', 10, 2);
记得将email_form
更改为您的联系表单名称,将email_123
更改为电子邮件字段名称。使用 WordPress 4.9.5 和 CF7 5.0.1 对我来说工作得很好
【讨论】:
复制到 function.php 并相应地创建表单。仍然无法正常工作,重复的电子邮件很容易进入表格,没有障碍。【参考方案2】:我尝试了很多解决方案,但很多都不适合我 最后决定更改按钮内容和背景颜色 10 秒(或任何你需要的)
必须为提交按钮设置 ID
[submit id:SendFormDataM "send your request"]
他们使用这个 jquery 代码(我已将其添加到 jquery.jvcf7_validation.js 文件中)
function submitPoll()
document.getElementById("SendFormDataM").style.backgroundColor = "#000000";
document.getElementById("SendFormDataM").value="please waiting";
setTimeout(function()
document.getElementById("SendFormDataM").style.backgroundColor = "#bc8a49";
document.getElementById("SendFormDataM").value="send your request";
, 10000);
var el = document.getElementById("SendFormDataM"); // use this if you have multi ID's
if(el)
el.addEventListener('click', submitPoll);
这会将背景颜色更改为黑色并写“请稍候”
10 秒后,按钮将恢复为带有旧内容的正常背景色
我试过禁用按钮,但它没有发送表单数据
【讨论】:
【参考方案3】:找了半天想找一个可以独立于保存数据库联系人的插件使用的功能代码,我想出了下面的代码。
/*We created the filter*/
add_filter( 'wpcf7_validate', 'email_already_in_db', 10, 2 );
/*We created the function*/
function email_already_in_db ( $result, $tags )
// We take the information from the form being submitted
$form = WPCF7_Submission::get_instance(); /*Here is the form ID of the Contact Form*/
$email = $form->get_posted_data('email'); /*Here is the email field*/
date_default_timezone_set('America/Sao_Paulo'); /*We set the time zone*/
$datetoday = date("Y-m-d"); /*We take the current date in the format that the contact plugin records the date*/
global $wpdb;
/*We make a select in the table where the contacts are recorded, checking if the email informed already exists on today's date */
$entry = $wpdb->get_results( "SELECT * FROM wp_db7_forms WHERE form_value LIKE '%$email%' AND form_date LIKE '%$datetoday%'" );
// If the select query in the database is positive (the email exists on the current date), it returns the error in the form, in the email field, not sending
if (!empty($entry))
$result->invalidate('email', 'Email already exists');
return $result;
我基于以下内容构建了我的版本: https://www.stacknoob.com/s/6X5Lisxm3DE87aGby3NzQZ
【讨论】:
【参考方案4】:我创建了自己的问题解决方案.. 只需根据您的表格替换
DB_PREFIX
和FORM_ID
...检查一下,我希望这会对您有所帮助
function is_already_submitted($formPostId, $fieldName, $fieldValue)
global $wpdb;
/*We make a select in the table where the contacts are recorded, checking if the email informed already exists on today's date */
$entry = $wpdb->get_results( "SELECT * FROM DB_PREFIX_db7_forms WHERE form_value LIKE '%$fieldValue%' AND form_post_id = '$formPostId'" );
// If the select query in the database is positive (the email exists on the current date), it returns the error in the form, in the email field, not sending
$found = false;
if (!empty($entry))
$found = true;
return $found;
function my_validate_email($result, $tag)
$formPostId = 'FORM_ID'; // Change to name of the form containing this field
$fieldName = 'your-email'; // Change to your form's unique field name
$errorMessage = 'This email address is already registered'; // Change to your error message
$name = $tag['name'];
if ($name == $fieldName)
if (is_already_submitted($formPostId, $fieldName, $_POST[$name]))
$result->invalidate($tag, $errorMessage);
return $result;
add_filter('wpcf7_validate_email*', 'my_validate_email', 10, 2);
【讨论】:
【参考方案5】:关注相关的CF7插件。在我的情况下,重复的表单提交是由 Jquery Validation For Contact Form 7 引起的。
【讨论】:
以上是关于联系表格 7 防止重复字段值提交的主要内容,如果未能解决你的问题,请参考以下文章