Get country name when used Pipe
-
I have followed this doc https://contactform7.com/selectable-recipient-with-pipes to send email according to selected country. Now i am trying to save form information in other database. all fields are saved properly, but only country name not able to get there.
What i have tried to save information, you can see on this link
https://stackoverflow.com/questions/67699300/contact-form-7-country-name-with-pipe-symbol-not-work-after-post-requestThe page I need help with: [log in to see the link]
-
// 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));
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?
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
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
How do i get $posted_data array?
I have tried this$posted_data = $form_to_DB->get_posted_data();
but not get luckyes 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 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
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));
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)
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 reply was modified 3 years, 6 months ago by deepakchaudhary.
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.
- The topic ‘Get country name when used Pipe’ is closed to new replies.