Contactform7 防止重复字段值提交

问题描述 投票:0回答:6

我正在使用 WordPress 3.8 和带有联系表单 7 数据库扩展的联系表单 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 表单提交。

php wordpress duplicates contact-form-7
6个回答
0
投票

我找到了解决方案。只需将此代码添加到您的 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 工作得很好


0
投票

我尝试了很多解决方案,但很多都不适合我 最后决定更改按钮内容和背景颜色 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秒后,按钮将回到正常背景颜色并显示旧内容

我尝试禁用该按钮,但它没有发送表单数据


0
投票

经过很长时间试图找到一个可以独立于在数据库中保存联系人的插件使用的功能代码,我想出了下面的代码。

/*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


0
投票

我创建了自己的问题解决方案..只需根据您的表格替换

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

0
投票

我也面临着同样的问题。并尝试了很多代码。最后,我找到了解决方案。请在这里找到代码。

第1步:安装插件https://wordpress.org/plugins/advanced-cf7-db/

第2步:在function.php中添加此代码

// Check mail address already in Database - start

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 wp_cf7_vdata_entry WHERE value LIKE '%$fieldValue%' AND cf7_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 = '462'; // Change to ID of the form containing this field
    $fieldName = 'Email-sub'; // 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);

// Check mail address already in Database - end

-1
投票

关注CF7相关插件。就我而言,重复的表单提交是由联系表单 7 的 Jquery 验证引起的。

© www.soinside.com 2019 - 2024. All rights reserved.