Contactform7可防止重复字段值提交

时间:2014-05-09 12:01:15

标签: php wordpress duplicates contact-form-7

我正在使用WordPress 3.8和联系表格7插件,联系表格7 db extension。

我想在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表单提交。

5 个答案:

答案 0 :(得分:0)

注意相关的CF7插件。在我的情况下,重复表单提交是由联系表格7的Jquery验证引起的。

答案 1 :(得分: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

为我工作得很好

答案 2 :(得分: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秒后,按钮将恢复为具有旧内容的正常背景颜色

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

答案 3 :(得分: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

答案 4 :(得分:0)

我创建了自己的问题解决方案。只需根据您的表格替换DB_PREFIXFORM_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);