Skip to main content

Import Values From Database Tables: Formidable Forms

Have you ever needed to Import Values From Database Tables within your WordPress installation and insert them in a Formidable Form field? We have! The tutorial below explains how to do just that. Target any WordPress database table, perform a calculation on that table and then insert the calculated value as the default value in a Formidable form field.

Furthermore this example will also allow you to filter the values calculated by the currently logged in user.

This article assumes the following:

  1. A basic understanding of MySQL and WordPress database tables
  2. A basic understanding of database calculations
  3. How to add custom PHP scripts to your WordPress website.
// Get Remaining Shares
add_filter('frm_get_default_value', 'my_custom_default_value', 10, 2);
function my_custom_default_value($new_value, $field){
if($field->id == 25){ //change 25 to the ID of the field
global $wpdb;
$user = wp_get_current_user();
$new_value = $wpdb->get_var("SELECT SUM(Shares_Remaining) FROM UserSummaryByYear WHERE user_name='$user->user_login'");
}
return $new_value;
}

Important Points:

  1. Change 25 to the ID of the field you want to insert the value into.
  2. Change UserSummaryByYear to the name of the db table to wish to target
  3. Change Shares_Remaining to the name the column you wish to target.
  4. Change SUM to the type of calculations you wish to do.

Tip: This example checks the UserSummaryByYear table and the ‘user_name‘ column and compares it to the logged-in user’s username. Our calculation assumes that the current user’s username is in the UserSummaryByYear table and will only total up the Shares_Remaining columns based on that username. You can adjust this as required as you can use almost any database table and column summary type you wish here.

Summary

Hopefully this will help anyone using Formidable Pro and needs to calculate and import values from database tables to be used in form calculations or form entries.

If you liked this tutorial, visit our blog for more ways to improve your websites and workflow.

Enjoy.