首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何检查我的数据库中是否存在通过联系人表格7提交的电子邮件?

如何检查我的数据库中是否存在通过联系人表格7提交的电子邮件?
EN

Stack Overflow用户
提问于 2015-04-08 09:30:27
回答 2查看 11.9K关注 0票数 2

当客户透过联络表格7提交电邮时,我如何检查电邮是否已存在于我的资料库中,以及如何将通知讯息更改为“您的电邮已在我们的资料库中存在”

到目前为止,我已经尝试过使用before_send钩子,但当我单击提交时,页面就挂起了,并且没有确认消息。

下面是我的functions.php中的函数

代码语言:javascript
复制
add_action( 'wpcf7_before_send_mail', 'check_email' );

function check_email( $cf7 )
{
$email = $cf7->posted_data["email"];
if($email == 'Is in our database'){
echo ('Your email exists in our database');
}
}

谢谢你的帮忙

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-04-08 12:00:48

我在验证时添加了一个过滤器:

代码语言:javascript
复制
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_exists( $email ) ) // email_exists is a WP function
        $result->invalidate('your-email', 'Your email exists in our database');
    // return the filtered value
    return $result;
}

在本例中,电子邮件字段的名称为your-email

函数email_exists是处理此问题的本机WP函数。

票数 5
EN

Stack Overflow用户

发布于 2019-07-16 02:05:36

这是一个适合我的插件Advanced Contact form 7 DB的解决方案

代码语言:javascript
复制
function email_already_in_db ( $result, $tags ) {
    // Retrieve the posted form
    $form  = WPCF7_Submission::get_instance();
    $form_posted_data = $form->get_posted_data();

    // Get the field name that we want to check for duplicates.
    // I added 'unique' to the beginning of the field name in CF7
    // Checking for that with preg_grep
    $unique_field_name = preg_grep("/unique(\w+)/", array_keys($form_posted_data));

    // $unique_field_name comes back as array so the next three lines give us the key as a string
    reset($unique_field_name);
    $first_key = key($unique_field_name);
    $unique_field_name = $unique_field_name[$first_key];

    // Check the form submission unique field vs what is already in the database
    $email = $form->get_posted_data($unique_field_name);
    global $wpdb;
    $entry = $wpdb->get_results( "SELECT * FROM wp_cf7_vdata_entry WHERE name LIKE '$unique_field_name' AND value='$email'" );

    // If already in database, invalidate
    if (!empty($entry)) {
      $result->invalidate($field_name, 'Your email: '.$email.' already exists in our database.');
      }
    // return the filtered value
  return $result;
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29504337

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档