hello who can help me i can not retrieve the data of the selection field in my database. That's what I do following this example Saving contact form 7 data into custom db and not wordpress db but it doesn't work
CREATE TABLE test(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
city VARCHAR(50)
);
[select city "New York" "Miami" "London"] [submit "OK"]
function contactform7_before_send_mail( $form_to_DB ) {
//connexion to database
$mydb = new wpdb('locacarm_rent2','Sam2005','locacarm_rent2','localhost');;
$form_to_DB = WPCF7_Submission::get_instance();
if ( $form_to_DB )
$formData = $form_to_DB->get_posted_data();
$city = $formData['city'];
$mydb->insert( 'test', array('city' =>$city ), array('%s') );
}
remove_all_filters ('wpcf7_before_send_mail');
add_action( 'wpcf7_before_send_mail', 'contactform7_before_send_mail' );
Your data in the dropdown (select) is stored as an array. Try this. It's more concise and uses the $submission
which is already passed to before_send_mail
as the third parameter.
Keep in mind, that you should sanitize your input - even with a <select>
as someone could post the data without your form. Contact form 7 does not sanitize input fields for insertion into a database.
function contactform7_before_send_mail( $contact_form, $abort, $submission ) {
// connexion to database.
$mydb = new wpdb( 'locacarm_rent2', 'Sam2005', 'locacarm_rent2', 'localhost' );
if ( $submission ) {
$form_data = $submission->get_posted_data();
$city = sanitize_text_field( wp_unslash( $form_data['city'][0] ) );
$mydb->insert( 'test', array( 'city' => $city ) );
}
}
add_action( 'wpcf7_before_send_mail', 'contactform7_before_send_mail', 10, 3 );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.