Hi @maahfuz
I hope you’re well today!
Here is an example of a full working code:
<?php
add_action('forminator_form_after_save_entry', function ($form_id, $response) {
if ($form_id != 3010) { // Please change the form ID.
return;
}
if ($response && is_array($response)) {
if ($response['success']) { // Only if form submitted successfully.
$form_entry_data = Forminator_Form_Entry_Model::get_latest_entry_by_form_id($form_id);
if ($form_entry_data instanceof Forminator_Form_Entry_Model) {
$text_1 = $form_entry_data->get_meta('text-1', true);
$text_2 = $form_entry_data->get_meta('text-2', true);
// now save to DB
global $wpdb;
$table_name = $wpdb->prefix . 'form_data'; // Replace with your custom table name
$wpdb->insert(
$table_name,
array(
'text-1' => $text_1,
'text-2' => $text_2,
// Add more fields as needed
)
);
}
}
}
}, 10, 2);
It’s actually a combination of Patrick’s code to get data and yours to put it into custom DB table. I tested it and it works but there are some important conditions that must be met:
1. make sure that form ID is correctly set in this line
if ($form_id != 3010) { // Please change the form ID.
2. and that the db table not only already exists but it does have correct columns created; as in this part of the code
array(
'text-1' => $text_1,
'text-2' => $text_2,
// Add more fields as needed
we are saving data to DB fields “text-1” and “text-2” accordingly in the “wp_form_data” table; those must already exist – this fields (columns) must already exist; in other words, you need to have proper table structure already set up.
3. Most importantly, you need to have the “Store Submissions in Database” option enabled form’s “Settings” section;
About that 3rd point – that’s critical for this type of code but I’m not sure why do you need that code in the first place so question is: are you trying to just put the data into some custom table additionally for some other custom use or do you actually want to store it ONLY in that custom table instead but NOT store in core/default Forminator DB tables (that would require slightly different code indeed)?
Kind regards,
Adam