wordpress Contactform7防止提交重复字段值

tyg4sfes  于 2022-11-22  发布在  WordPress
关注(0)|答案(6)|浏览(244)

我正在使用WordPress 3.8和联系人表格7插件与联系人表格7db扩展。
我想检查现有的电子邮件,我做了一个钩子上提交(alter_wpcf7_posted_data)在functions.php如下:

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

izj3ouym

izj3ouym1#

我找到了解决这个问题的方法。只需将此代码添加到您的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

fkaflof6

fkaflof62#

我已经尝试了许多解决方案,但许多不与我合作,最后决定改变按钮内容和背景颜色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秒后,按钮将恢复为正常的背景色,并显示旧内容
我已尝试禁用该按钮,但它没有发送表单数据

3duebb1j

3duebb1j3#

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

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

zpjtge22

zpjtge224#

我创建了我自己的问题解决方案...只是根据您的形式替换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);
eit6fx6z

eit6fx6z5#

我也面临着同样的问题。并尝试了许多代码。最后,我找到了解决方案。请在这里找到代码。
步骤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
fdbelqdn

fdbelqdn6#

注意相关的CF7插件。在我的例子中,重复的表单提交是由Jquery Validation For Contact Form 7引起的。

相关问题