我使用的是WordPress 3.8和联系形式7插件与联系形式7数据库扩展。
我想要检查现有的电子邮件,我在functions.php中的submit on a hook (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表单提交。
发布于 2018-05-09 01:13:46
我找到了解决这个问题的办法。只需在您的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上工作得很好
发布于 2020-06-06 00:56:38
我尝试了许多解决方案,但许多不适合我的解决方案最终决定更改按钮内容和背景颜色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秒后,按钮将恢复正常的背景颜色,并显示旧内容
我已尝试禁用该按钮,但它没有发送表单数据
发布于 2020-09-06 09:49:33
经过很长一段时间的尝试,我找到了一个可以独立于插件使用的功能代码,可以将联系人保存在数据库中,我想出了下面的代码。
/*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构建我的版本
https://stackoverflow.com/questions/23564142
复制相似问题