Deepak3301086
Deepak3301086

Reputation: 487

Contact form 7 getting values from select with pipes

I am trying to get country name from country drop down, where I have used pipe symbol. like this in my form

[text* FullName id:fullname]
[select* country id:country include_blank "United States | [email protected]" "Canada | [email protected]" "Mexico | [email protected]" "United Kingdom | [email protected]"]

I have used different-2 email with country name because i need to sand email to company employee according to country name. In email template i have used [country] to get email and [_raw_country] to get country name that is working fine. I am trying to get country name in function.php file to save it in database by this way.

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' ) );
}

But it gives nothing. if i try to get like this

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

it gives me only email id like [email protected] I have tryed $country = $formData['country'][1]; It give me nothing. I have also tryed $country = $formData['_raw_country']; But not get luck.

This is the reference document https://contactform7.com/selectable-recipient-with-pipes/

Upvotes: 1

Views: 1937

Answers (2)

Erik
Erik

Reputation: 100

you need something like this:

<?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));

Upvotes: 3

Howard E
Howard E

Reputation: 5669

I posted this answer in your other question. This should work.

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' ) );
    }
}

Upvotes: 0

Related Questions