Add custom store function NinjaForms mysql

16 Views Asked by At

I am trying to create a custom function that stores form data in mysql database. Whenever i click the submit button, a new row is created in my mysql table but the column values are empty except for the id values.

add_action('ninja_forms_after_submission', 'custom_store_ninja_forms_data', 10, 1);

 function custom_store_ninja_forms_data($form_data) {
// Check if it's the specific form you want to target (form ID 14)
if ($form_data['form_id'] == 14) {
    global $wpdb;

    // Your custom table name
    $table_name = $wpdb->prefix . 'custom_form_data';

    // Customize this part to match your form field names
    $user_type = sanitize_text_field($form_data[ 'fields' ]['Vous êtes']);
    $last_name = sanitize_text_field($form_data[ 'fields' ]['field1']);
    $first_name = sanitize_text_field($form_data['fields']['Prénom']);
    $solution = sanitize_text_field($form_data['fields']['Quelle solution']);
    $npa = sanitize_text_field($form_data['fields']['NPA']);
    $canton = sanitize_text_field($form_data['fields']['Canton']);
    $country = sanitize_text_field($form_data['fields']['Pays']);
    $email = sanitize_email($form_data['fields']['E-mail']);
    $phone = sanitize_text_field($form_data['fields']['Tél.']);
    $user_request = sanitize_textarea_field($form_data['fields']['Votre demande']);
    $privacy_accepted = isset($form_data['fields']["J'accepte la politique de 
    confidentialité."]) ? 1 : 0;
    $data_protection_accepted = isset($form_data['fields']['Je suis d\'accord']) ? 1 : 0;


  
    // Insert data into the custom table
    $wpdb->insert(
        $table_name,
        array(
            'user_type' => $user_type,
            'last_name' => $last_name,
            'first_name' => $first_name,
            'solution' => $solution,
            'npa' => $npa,
            'canton' => $canton,
            'country' => $country,
            'email' => $email,
            'phone' => $phone,
            'user_request' => $user_request,
            'privacy_accepted' => $privacy_accepted,
            'data_protection_accepted' => $data_protection_accepted,
        )
    );
   }
    }
0

There are 0 best solutions below