Sending Bricks Form Data to a MySQL Table

Good morning

I use this code from the Bricks academy. Add the code into a WPCodeBox snippet or into the functions.php file.

I added the standard WP function “wpdb->insert” to the code, so Wordpress takes care of everything including preparing the data and sending the data as a new row to the database. Gotta love Wordpress, right?

How to do it:

  1. Replace the table name “wp_test” with your WP table name in the code.

  2. Replace the id number (the xxxxxx) in [‘form-field-xxxxxx’] in the code with the 6 digit id number from your Bricks form fields in the code.

function my_form_custom_action( $form ) {  
   $fields = $form->get_fields();
 
  // $formId = $fields['formId'];
  // $postId = $fields['postId'];
   $settings = $form->get_settings();
  // $files = $form->get_uploaded_files();
  
  // Perform some logic here...
   global $wpdb;
   $wpdb->insert("wp_test", array(
   "Name" => $fields['form-field-256ffd'],
   "Email" => $fields['form-field-d64e13'],
   "Message" => $fields['form-field-ea931b'],
  
));

  // Set result in case it fails
  $form->set_result([
    'action' => 'my_custom_action',
    'type'    => 'success', //or danger or info
    'message' => esc_html__('Oh my custom action failed', 'bricks'),
  ]);
}
add_action( 'bricks/form/custom_action', 'my_form_custom_action', 10, 1 );

As far as I understand, this custom action is called when any form is submitted that has the custom action trigger. So if you have more than 1 form with the custom action trigger, you would have to also make a check that the database is only updated if the specific form for that case is submitted.

See this discussion for reference:

Cheers

Patric

2 Likes