【问题标题】:Contact form 7 getting values from select with pipes联系表格 7 使用管道从选择中获取值
【发布时间】:2022-03-23 17:52:43
【问题描述】:

我正在尝试从使用管道符号的国家下拉列表中获取国家/地区名称。像我这样的形式

[text* FullName id:fullname]
[select* country id:country include_blank "United States | a@gmail.com" "Canada | a@gmail.com" "Mexico | b@gmail.com" "United Kingdom | c@gmail.com"]

我使用了带有国家名称的不同 2 电子邮件,因为我需要根据国家名称将电子邮件发送给公司员工。在电子邮件模板中,我使用[country] 获取电子邮件并使用[_raw_country] 获取工作正常的国家名称。 我正在尝试在 function.php 文件中获取国家名称以通过这种方式将其保存在数据库中。

function contactform7_before_send_mail( $form_to_DB ) {
    //set your db details
    $mydb = new wpdb('user','password','databasename','localhost');    
    $form_to_DB = WPCF7_Submission::get_instance();
    if ( $form_to_DB ) 
        $formData = $form_to_DB->get_posted_data();        
    $fullname = $formData['FullName'];    
    $country = $formData['country'];

    $mydb->insert( 'mytable', array( 'fullname' =>$fullname,'country' =>$country), array( '%s' ) );
}

但它什么也没给。如果我试着变成这样

$country = $formData['country'][0];

它只给我电子邮件 ID,例如 a@gmail.com 我试过 $country = $formData['country'][1]; 它什么也没给我。我也试过$country = $formData['_raw_country']; 但运气不好。

这是参考文件 https://contactform7.com/selectable-recipient-with-pipes/

【问题讨论】:

  • 您可以尝试使用此stackoverflow.com/questions/60361457/… 将您的表单数据发送到错误日志并查看结果。
  • ["FullName"]=> string(6) "Deepak" ["country"]=> array(1) { [0]=> string(21) "a@gmail.com" }
  • 我已经按照你的建议 ob_start(); var_dump($formData); error_log(ob_get_clean());并在上面的评论中得到输出。
  • 这个问题是否与您的其他问题重复(某种程度上)?

标签: wordpress contact-form-7


【解决方案1】:

你需要这样的东西:

<?php
// Get the submitted data
$submission = WPCF7_Submission::get_instance();

if ( ! $submission
     or ! $posted_data = $submission->get_posted_data() ) {
    return;
}

// Get the contact form additional data
$contact_form = $submission->get_contact_form();

// get the tag used in the form
$mail_tags=$contact_form->scan_form_tags();

// search into tags for country
$key = array_search('country', array_column($mail_tags, 'name'));

// get the country pipes values
$pipes = new WPCF7_Pipes( $mail_tags[$key]->raw_values );

// get an array with the pipes
$pipes_array = $pipes->to_array();

// get the choosen value
$pipes_val = array_search( $posted_data['country'][0], array_column($pipes_array, 1));

// finally!
error_log(print_r($posted_data['country'],true));
error_log(print_r("in",true));
error_log(print_r($pipes_array[$pipes_val][0],true));

【讨论】:

  • 我试过 error_log(print_r($mail_tags[$key],true));它用于国家标签。和输出是这样的
  • 我已经在这里wordpress.org/support/topic/get-country-name-when-used-pipe/… 发布了我的输出,它只给了我国家下拉列表的所有细节,除了选定的国家细节。
  • ciao deepak,我已经更新了答案以获取管道值数组
  • 它给了我所有值来代替选中的值。
  • $posted_data['country'] 末尾缺少 [0]!固定
【解决方案2】:

我在您的另一个问题中发布了这个答案。这应该可以。

add_action( 'wpcf7_before_send_mail', 'cf7_pipes_so_67686211');
function cf7_pipes_so_67686211( $contact_form ) {
    global $wpdb;
    $mydb = new wpdb('user','password','database','localhost');
    
    // Get Form Tags
    $tags = $contact_form->scan_form_tags();
    $pipe_array = array();
    // Find the Values of the Pipes
    foreach ($tags as $tag){
        if ($tag->name === 'primarybusiness'){
           $pipe_array = $tag->pipes->to_array();
        }
    }
    // Get the form data
    $form_to_DB = WPCF7_Submission::get_instance();
    if ( $form_to_DB ) {
        $formData = $form_to_DB->get_posted_data();
        $fullname = $formData['FullName'];
        foreach ($pipe_array as $value) {
            // Compare and find the pipe value
            if ( $posted_data['primarybusiness'][0] === $value[1] ){
                $primarybusiness = $value[0];
            }
        }
        $mydb->insert( 'mytable', array( 'fullname'=>$fullname,'primarybusiness'=>$primarybusiness), array( '%s' ) );
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-20
    • 2018-06-09
    • 2017-05-25
    • 2021-12-05
    • 2021-11-14
    • 2015-09-22
    • 2023-04-06
    相关资源
    最近更新 更多