Viewing 15 replies - 1 through 15 (of 16 total)
  • // 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 | [email protected]
                [1] => Canada | [email protected]
                [2] => Mexico | [email protected]
                [3] => United Kingdom | [email protected]
                [4] => Afghanistan | [email protected]
                [5] => Albania | [email protected]
                [6] => Algeria | [email protected]
                
            )
    
        [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] => [email protected]
                            )
    
                        [1] => WPCF7_Pipe Object
                            (
                                [before] => Canada
                                [after] => [email protected]
                            )
    
                        [2] => WPCF7_Pipe Object
                            (
                                [before] => Mexico
                                [after] => [email protected]
                            )
    
                        [3] => WPCF7_Pipe Object
                            (
                                [before] => United Kingdom
                                [after] => [email protected]
                            )
    
                    )
    
            )
    
        [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 3 years, 6 months ago by Yui.
    • This reply was modified 3 years, 6 months ago by Yui. Reason: please use CODE button for proper formatting

    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] => [email protected]
            )
    
        [1] => Array
            (
                [0] => Canada
                [1] => [email protected]
            )
    
        [2] => Array
            (
                [0] => Mexico
                [1] => [email protected]
            )
    
        [3] => Array
            (
                [0] => United Kingdom
                [1] => [email protected]
            )
    )

    I have selected Mexico.

    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

    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 3 years, 6 months ago by Erik.
    • This reply was modified 3 years, 6 months ago by Erik.
    • This reply was modified 3 years, 6 months 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));

    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 3 years, 6 months ago by Erik. Reason: formatting
    Thread Starter deepakchaudhary

    (@deepakchaudhary)

    I have copy past your code and get output this

    Array
    (
        [0] => [email protected],[email protected]
    )
    
    United States

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

    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] => [email protected]
    )
    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.

    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|[email protected]” “Canada|[email protected]” “Mexico|[email protected]”]

    that outputs (with mexico selected):
    [email protected]
    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|[email protected]” “Canada|[email protected]” “Mexico|[email protected]” “India|[email protected]” “Sri Lanka|[email protected]” “Pakistan|[email protected],[email protected]” “China|[email protected],[email protected]”]

    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 3 years, 6 months 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.