联系表7,保存选定选项(下拉菜单)到数据库

发布于 2025-01-24 10:08:56 字数 1375 浏览 3 评论 0原文

我正在尝试保存到我的数据库,即我的联系表7表格中的提交。我发现了一些功能(不知道作者)。它有效,但不能保存从下拉菜单中选择的选项到数据库。我不知道我缺少什么。我尝试了这一点:

add_action("wpcf7_submit", "SE_379325_forward_cf7", 10, 2);

function SE_379325_forward_cf7($form, $result) {
 if( !class_exists('WPCF7_Submission') )
  return;
 $submission = WPCF7_Submission::get_instance();
 if ($result["status"] == "mail_sent") { // proceed only if email has been sent 
  $posted_data = $submission->get_posted_data();
  save_posted_data($posted_data);
 }
};


// your insert function:
function save_posted_data($posted_data){
$form_id = $posted_data["_wpcf7"]; // this is the post->ID of the submitted form so if you have more than one you can decide whether or not to save this form fields
if($form_id == 403)
 return;
 global $wpdb;

$wpdb->insert( 
  $wpdb->prefix.'tabletest',
  array(
    'lastname'=>$posted_data['lastName'],
    'name'=>$posted_data['firstName'],
    'email'=>$posted_data['email'],
    'phone'=>$posted_data['tel-3'],
    'subject'=>$posted_data['menu-338'],
    'role'=>$posted_data['menu-761'],
    'message'=>$posted_data['your-message'],
    'thedate'=>$posted_data['date-288']
  ),
  array('%s')
 );
} 

我无法保存的字段是“主题”和“角色”。它们只是在我的表列上空的。

这是我从联系表7 这是我的表格

I am trying to save to my database, the submissions from my contact form 7 form. I found some functions (don't know the author) to do so. It works, but it won't save the option selected from the drop-down menus to the database. I don't know what I am missing. I tried this:

add_action("wpcf7_submit", "SE_379325_forward_cf7", 10, 2);

function SE_379325_forward_cf7($form, $result) {
 if( !class_exists('WPCF7_Submission') )
  return;
 $submission = WPCF7_Submission::get_instance();
 if ($result["status"] == "mail_sent") { // proceed only if email has been sent 
  $posted_data = $submission->get_posted_data();
  save_posted_data($posted_data);
 }
};


// your insert function:
function save_posted_data($posted_data){
$form_id = $posted_data["_wpcf7"]; // this is the post->ID of the submitted form so if you have more than one you can decide whether or not to save this form fields
if($form_id == 403)
 return;
 global $wpdb;

$wpdb->insert( 
  $wpdb->prefix.'tabletest',
  array(
    'lastname'=>$posted_data['lastName'],
    'name'=>$posted_data['firstName'],
    'email'=>$posted_data['email'],
    'phone'=>$posted_data['tel-3'],
    'subject'=>$posted_data['menu-338'],
    'role'=>$posted_data['menu-761'],
    'message'=>$posted_data['your-message'],
    'thedate'=>$posted_data['date-288']
  ),
  array('%s')
 );
} 

The fields I cannot save are 'subject' and 'role'. They are just empty on my table columns.

This is my form from Contact Form 7

I would appreciate any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉城凉梦凉人心 2025-01-31 10:08:56

两个潜在的问题。

  1. 表单ID可能不像您试图获得它的方式可见。您可以通过在第一个提交函数中获取$ contact_form-> id将其传递给您的功能。
  2. 从CF7中的几个版本开始,下拉列表中的数据作为数组(始终)传递。

我也添加了消毒,您可以根据需要进行更改。默认情况下,CF7不会对数据进行消毒,因为它只是输入/输入输出,并且不会存储任何地方。

add_action( 'wpcf7_submit', 'SE_379325_forward_cf7', 10, 2 );
function SE_379325_forward_cf7( $form, $result ) {
    if ( ! class_exists( 'WPCF7_Submission' ) ) {
        return;
    }
    $submission = WPCF7_Submission::get_instance();
    if ( 'mail_sent' === $result['status'] ) { // proceed only if email has been sent.
        $posted_data = $submission->get_posted_data();
        save_posted_data( $posted_data, $form->id );
    }
}
// your insert function.
function save_posted_data( $posted_data, $form_id ) {
    if ( 403 !== $form_id ) {
        global $wpdb;
        $wpdb->insert(
            $wpdb->prefix . 'tabletest',
            array(
                'lastname' => sanitize_text_field( wp_unslash( $posted_data['lastName'] ) ),
                'name'     => sanitize_text_field( wp_unslash( $posted_data['firstName'] ) ),
                'email'    => sanitize_text_field( wp_unslash( $posted_data['email'] ) ),
                'phone'    => sanitize_email( wp_unslash( $posted_data['tel-3'] ) ),
                'subject'  => sanitize_text_field( wp_unslash( $posted_data['menu-338'][0] ) ),
                'role'     => sanitize_text_field( wp_unslash( $posted_data['menu-761'][0] ) ),
                'message'  => sanitize_textarea_field( wp_unslash( $posted_data['your-message'] ) ),
                'thedate'  => sanitize_text_field( wp_unslash( $posted_data['date-288'] ) ),
            ),
            array( '%s' )
        );
    }
}

Two potential problems.

  1. The form ID is probably not visible the way you're trying to get it. You can pass that to your function by getting the $contact_form->id in the first submit function.
  2. The data from the dropdowns is passed as an array (always) as of a few versions ago in CF7.

I've added sanitization as well, you can alter as needed. CF7 Doesn't by default sanitize data since it's just input in/input out, and not stored anywhere.

add_action( 'wpcf7_submit', 'SE_379325_forward_cf7', 10, 2 );
function SE_379325_forward_cf7( $form, $result ) {
    if ( ! class_exists( 'WPCF7_Submission' ) ) {
        return;
    }
    $submission = WPCF7_Submission::get_instance();
    if ( 'mail_sent' === $result['status'] ) { // proceed only if email has been sent.
        $posted_data = $submission->get_posted_data();
        save_posted_data( $posted_data, $form->id );
    }
}
// your insert function.
function save_posted_data( $posted_data, $form_id ) {
    if ( 403 !== $form_id ) {
        global $wpdb;
        $wpdb->insert(
            $wpdb->prefix . 'tabletest',
            array(
                'lastname' => sanitize_text_field( wp_unslash( $posted_data['lastName'] ) ),
                'name'     => sanitize_text_field( wp_unslash( $posted_data['firstName'] ) ),
                'email'    => sanitize_text_field( wp_unslash( $posted_data['email'] ) ),
                'phone'    => sanitize_email( wp_unslash( $posted_data['tel-3'] ) ),
                'subject'  => sanitize_text_field( wp_unslash( $posted_data['menu-338'][0] ) ),
                'role'     => sanitize_text_field( wp_unslash( $posted_data['menu-761'][0] ) ),
                'message'  => sanitize_textarea_field( wp_unslash( $posted_data['your-message'] ) ),
                'thedate'  => sanitize_text_field( wp_unslash( $posted_data['date-288'] ) ),
            ),
            array( '%s' )
        );
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文