当客户通过联系表格7提交电子邮件时, 如果来自db的联系人存在两次以上相同的电子邮件ID。需要显示错误消息(就像你已经提交了2次以上)
例如如果test@gmail.com已经通过联系表格7和联系表格7 db提交了两次。不允许再次提交
我试过这段代码
add_filter( 'wpcf7_validate', 'email_already_in_db', 10, 2 );
function email_already_in_db ( $result, $tags ) {
// retrieve the posted email
$form = WPCF7_Submission::get_instance();
$email = $form->get_posted_data('your-email');
// if already in database, invalidate
if( email_already_in_database( $email ) )
$result->invalidate('your-email', 'Your email exists in our database');
// return the filtered value
return $result; }
但是电子邮件的简单验证也不起作用,当我在function.php中使用此代码时,在提交后,加载器继续加载。不提交数据。
答案 0 :(得分:1)
查看此插件,
https://github.com/mdsimpson/contact-form-7-to-database-extension
之后,如果没有插入,请包含此脚本
https://cfdbplugin.com/?page_id=904
/**
* @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);
答案 1 :(得分:0)
我有一个更好的解决方案,不需要任何服务器交互。每次用户发送电子邮件时,为什么不在本地存储发送的电子邮件详细信息,然后在客户端进行比较,而不是查询数据库?
使用JavaScript,您可以将每封已发送电子邮件中的信息添加到sessionStorage
或localStorage
中变量内的数组中。然后,如果该用户尝试发送另一封电子邮件,则会检查以前发送的所有电子邮件的内容,以确保其不重复。