Elementor Pro Form Widget Submission returns parsererror

47 viewselementorformsphppluginswordpress
0

i created a WordPress plugin for Elementor Pro form, which will capture the form submission data and send that data to other db then proceed and also save the response in elementor form submission.

now the issue is when the form is submitted it submitted well but a parseerror show below the form instead of the Thank you! success message. i also enabled the custom message option in the form widget.

here’s the code plugin code

<?php

/**
 * Plugin Name: Elementor API Capture
 * Description: Custom plugin to capture Elementor form submissions and send to API.
 * Version: 1.1
 */

// Hook into Elementor form submission
add_action('wp_ajax_elementor_pro_forms_send_form', 'capture_elementor_form_submission');
add_action('wp_ajax_nopriv_elementor_pro_forms_send_form', 'capture_elementor_form_submission');

function capture_elementor_form_submission()
{
    // Access the form data submitted by Elementor
    $post_data = $_POST['form_fields'];
    // print_r($post_data);die;

    /*
     * 🔺 Your API Request Logic Here 🔺
     * Prepare data to send to the API
     */

    $siteName = $_SERVER['SERVER_NAME'];
    // echo $siteName;
    $post_data['source'] = $siteName;
    // print_r($post_data);die;

    // Make the API request
    $response = wp_remote_post('api-end-point', array(
        'body' => $post_data
        // 'headers' => array('Content-Type' => 'application/json'),
    ));

    // print_r($response);die;

    // Check for API request success
    if (is_wp_error($response)) {
        // Handle API request error
        echo 'API Request Error: ' . $response->get_error_message();
    } else {
        // API request successful
        $api_response = json_decode(wp_remote_retrieve_body($response), true);
        echo 'API Response: ' . json_encode($api_response);
    }
    /*
     * 🔺 END HERE 🔺
    */

    // Continue with Elementor's form submission handling
    include_once(plugin_dir_path(__FILE__) . 'elementor-pro/modules/forms/classes/ajax-handler.php');
    $ajax_handler = new ElementorProModulesFormsClassesAjax_Handler();
    $ajax_handler->ajax_send_form();

    // Make sure to exit to prevent further processing of Elementor's form submission
    exit;
}

response after form submission

i created a WordPress plugin for Elementor Pro form, which will capture the form submission data and send that data to other db then proceed and also save the response in elementor form submission.

now when the form is submitted the form is submitted and data is also pushed to the database but on the front-end instead of showing the success message below the form it shows the parseerror.

i enabled the custom form message option in the Elementor Pro Form Widget.