• Resolved maahfuz

    (@maahfuz)


    Hi, I am new to wordpress theme development. I am using forminator to catch user’s data submission in my custom theme. I want to catch the form data and make entry to my custom database table. I have used API to get data and also used forminator_custom_form_submit hook for submission. However the data was not sent to my database table.

    • This topic was modified 10 months, 2 weeks ago by maahfuz.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter maahfuz

    (@maahfuz)

    I have used following code in my functions.php. But it doesn’t work.

    function handle_forminator_form_submission($response, $form_id, $data) {
    
    ? ? ? ? ? ? // Check if the form submission was successful
    
    ? ? ? ? ? ? error_log('Forminator form submission triggered');
    
    ? ? ? ? ? ? if ($response['success']) {
    
    ? ? ? ? ? ? ? ? // Get submitted form data
    
    ? ? ? ? ? ? ? ? $form_data = $data['data'];
    
    ? ? ? ? ? ? ? ? // Extract data from Forminator form
    
    ? ? ? ? ? ? ? ? $text_1 = isset($form_data['text-1']) ? sanitize_text_field($form_data['text-1']) : '';
    
    ? ? ? ? ? ? ? ? $text_2 = isset($form_data['text-2']) ? sanitize_text_field($form_data['text-2']) : '';
    
    ? ? ? ? ? ? ? ? // Insert form data into custom database table
    
    ? ? ? ? ? ? ? ? global $wpdb;
    
    ? ? ? ? ? ? ? ? $table_name = $wpdb->prefix . 'form_new'; // Replace with your custom table name
    
    ? ? ? ? ? ? ? ? $wpdb->insert(
    
    ? ? ? ? ? ? ? ? ? ? $table_name,
    
    ? ? ? ? ? ? ? ? ? ? array(
    
    ? ? ? ? ? ? ? ? ? ? ? ? 'text_1' => $text_1,
    
    ? ? ? ? ? ? ? ? ? ? ? ? 'text_2' => $text_2,
    
    ? ? ? ? ? ? ? ? ? ? ? ? // Add more fields as needed
    
    ? ? ? ? ? ? ? ? ? ? )
    
    ? ? ? ? ? ? ? ? );
    
    ? ? ? ? ? ? }
    
    ? ? ? ? }
    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @maahfuz

    I hope you are doing well.

    Can you please try the following:

    <?php
    
    add_action('forminator_form_after_save_entry', function ($form_id, $response) {
        if ($form_id != 1609) { // 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);
                    // rest of the code goes here.
                }
            }
        }
    }, 10, 2);

    You don’t need to use the sanitize_text_field() as Forminator will do it before adding it to the database so when you use the get_latest_entry_by_form_id() it will be the proper data.

    Let us know the result you got.
    Best Regards
    Patrick Freitas

    Thread Starter maahfuz

    (@maahfuz)

    Thanks Patrick Freitas.
    I have used your code but it still doesn’t save data to my database table. Could you please give an example how can I save this data to my database table. Suppose the table name is wp_form_data

    Best Regards
    maahfuz

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    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

    Plugin Support Dmytro – WPMU DEV Support

    (@wpmudevsupport16)

    Hello @maahfuz,

    I hope you’re doing good today!

    Since haven’t heard from you for a while, I’m marking this thread as resolved.

    Please feel free to reply in case you still have questions, or need further help.

    Best Regards,
    Dmytro

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Not able to submit form data in custom dastabse table with forminator hook’ is closed to new replies.