Support » Plugin: Contact Form 7 » Get country name when used Pipe

Viewing 15 replies - 1 through 15 (of 16 total)
  • Erik

    (@codekraft)

    // 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();

    $key = array_search(‘your tag name’, array_column($mail_tags, ‘name’));

    error_log(print_r($mail_tags[$key], true));

    Thread Starter deepakchaudhary

    (@deepakchaudhary)

    I have tried error_log(print_r($mail_tags[$key],true)); it is for country tag. and output come like this

    
    WPCF7_FormTag Object
    (
        [type] => select*
        [basetype] => select
        [name] => country
        [options] => Array
            (
                [0] => id:country
                [1] => include_blank
            )
    
        [raw_values] => Array
            (
                [0] => United States | a@example.com.com
                [1] => Canada | a@example.com.com
                [2] => Mexico | a@example.com.com
                [3] => United Kingdom | a@example.com.com
                [4] => Afghanistan | a@example.com.com
                [5] => Albania | a@example.com.com
                [6] => Algeria | a@example.com.com
                
            )
    
        [values] => Array
            (
                [0] => United States
                [1] => Canada
                [2] => Mexico
                [3] => United Kingdom
                [4] => Afghanistan
                [5] => Albania
                
            )
    
        [pipes] => WPCF7_Pipes Object
            (
                [pipes:WPCF7_Pipes:private] => Array
                    (
                        [0] => WPCF7_Pipe Object
                            (
                                [before] => United States
                                [after] => a@example.com.com
                            )
    
                        [1] => WPCF7_Pipe Object
                            (
                                [before] => Canada
                                [after] => a@example.com.com
                            )
    
                        [2] => WPCF7_Pipe Object
                            (
                                [before] => Mexico
                                [after] => a@example.com.com
                            )
    
                        [3] => WPCF7_Pipe Object
                            (
                                [before] => United Kingdom
                                [after] => a@example.com.com
                            )
    
                    )
    
            )
    
        [labels] => Array
            (
                [0] => United States
                [1] => Canada
                [2] => Mexico
                [3] => United Kingdom
                [4] => Afghanistan
                [5] => Albania
                [6] => Algeria
                
            )
    
        [attr] => 
        [content] => 
    )
    

    How to get selected country name?

    • This reply was modified 1 year, 1 month ago by Yui.
    • This reply was modified 1 year, 1 month ago by Yui. Reason: please use CODE button for proper formatting
    Erik

    (@codekraft)

    you can get the array items with raw / values in this way:

    $pipes = new WPCF7_Pipes( $mail_tags[$key]->raw_values );
    error_log(print_r($pipes->to_array(),true));

    use the submitted data if you need the key of the selected value

    Thread Starter deepakchaudhary

    (@deepakchaudhary)

    this is what i try.

    $form_to_DB = WPCF7_Submission::get_instance();
    $contact_form = $form_to_DB->get_contact_form();
    $mail_tags=$contact_form->scan_form_tags();
    $key = array_search('country', array_column($mail_tags, 'name'));
    $pipes = new WPCF7_Pipes( $mail_tags[$key]->raw_values );
    error_log(print_r($pipes->to_array(),true));

    this give me full list not only selected one.

    Array
    (
        [0] => Array
            (
                [0] => United States
                [1] => tukateam@tukatech.com
            )
    
        [1] => Array
            (
                [0] => Canada
                [1] => tukateam@tukatech.com
            )
    
        [2] => Array
            (
                [0] => Mexico
                [1] => tukateam@tukatech.com
            )
    
        [3] => Array
            (
                [0] => United Kingdom
                [1] => tukateam@tukatech.com
            )
    )

    I have selected Mexico.

    Erik

    (@codekraft)

    use the submitted data if you need the key of the selected value as:

    $posted_data[‘country’]

    use array_search OR loop into array

    Thread Starter deepakchaudhary

    (@deepakchaudhary)

    How do i get $posted_data array?
    I have tried this $posted_data = $form_to_DB->get_posted_data(); but not get luck

    Erik

    (@codekraft)

    yes in that way!

    check the first part of the code i send

    // Get the submitted data
    $submission = WPCF7_Submission::get_instance();
    
    if ( ! $submission
    or ! $posted_data = $submission->get_posted_data() ) {
    return;
    }

    search for posted_data[‘country’] into array $pipes->to_array() with array search
    array_search(posted_data['country'], array_column($pipes->to_array(), 0));

    • This reply was modified 1 year, 1 month ago by Erik.
    • This reply was modified 1 year, 1 month ago by Erik.
    • This reply was modified 1 year, 1 month ago by Erik.
    Thread Starter deepakchaudhary

    (@deepakchaudhary)

    This is what i hve tried.
    It give me nothing.

    $form_to_DB = WPCF7_Submission::get_instance();
        if ( $form_to_DB ) 
            $formData = $form_to_DB->get_posted_data();
    $contact_form = $form_to_DB->get_contact_form();
    $mail_tags=$contact_form->scan_form_tags();
    $key = array_search('country', array_column($mail_tags, 'name'));
    $pipes = new WPCF7_Pipes( $mail_tags[$key]->raw_values );
    $country = array_search($formData['country'], array_column($pipes->to_array(), 0));
    Erik

    (@codekraft)

    array search returns the key of the array

    
    // 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'], array_column($pipes_array, 1));
    
    // finally!
    error_log(print_r($posted_data['country'],true));
    error_log(print_r($pipes_array[$pipes_val][0],true));
    
    • This reply was modified 1 year ago by Erik. Reason: formatting
    Thread Starter deepakchaudhary

    (@deepakchaudhary)

    I have copy past your code and get output this

    Array
    (
        [0] => umg@example.com,ab@example.com
    )
    
    United States

    I have selected India, So email id which I got, is related to india. but country name India is still not display.

    Erik

    (@codekraft)

    this time I have checked, sry! 😅

    // 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 chosen 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));
    Thread Starter deepakchaudhary

    (@deepakchaudhary)

    This output i got.

    Array
    (
        [0] => a@example.com
    )
    United States

    Email id is ok, But first name of country displayed from list, in place of selected one.I was selected Mexico
    email id is same of first country and the one which i was selected.
    Email id can be same for 50 country name in list. So if you try to get county name according to selected value “email id”, It will not be possible.

    Erik

    (@codekraft)

    that sound strange because in the line before we have search into array
    you can use $posted_data[‘country’][0] the value that we have search into array.

    my test field is this
    [select* country “United States|america@example.com” “Canada|canada@example.com” “Mexico|mexico@example.com”]

    that outputs (with mexico selected):
    mexico@example.com
    in
    Mexico

    // 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();
    error_log(print_r($pipes_array,true));
    
    // get the chosen value
    $pipes_val = array_search( $posted_data['country'][0], array_column($pipes_array, 1));
    
    // finally!
    error_log(print_r($posted_data['country'][0],true));
    error_log(print_r("in",true));
    error_log(print_r($pipes_array[$pipes_val][0],true));

    please post the result of the log with this last code (now it prints the pipes)

    Thread Starter deepakchaudhary

    (@deepakchaudhary)

    My friend. it is not like you have. it is like this
    [select* country “United States|usteam@example.com” “Canada|usteam@example.com” “Mexico|indiateam@example.com” “India|indiateam@example.com” “Sri Lanka|indiateam@example.com” “Pakistan|indiateam@example.com,usteam@example.com” “China|indiateam@example.com,otherteam@example.com”]

    Erik

    (@codekraft)

    this was caused due I use search for the email field, but the same email were used for different fields.

    I’ll think of a solution and write it down for you!

    • This reply was modified 1 year ago by Erik.
Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Get country name when used Pipe’ is closed to new replies.