I will be discussing about how to fetch values from “Data from Entries” field using Formidable API.
Data from Entries field type is a brilliant offering from Formidable Pro (FP) team, which at the very basic can display field values dynamically (let’s consider drop-down for this example) from other form entries.
Let’s consider two forms: FormA (source) and FormB (target). For this example, let’s assume FormA consists of one field “Name” and has two entries created. FormB has one Data From Entries field “Name” and is configured to display the Names from FormA as a drop-down. Ergo, the Name field in FormB will display two names, let’s assume the names are John and Jane Doe.
On previewing FormB, it will paint the drop-down list with the two names as expected. However, on analyzing the HTML structure in detail (I use Firebug), it is revealed to be somewhat similar as shown below.
<div id="frm_data_field_<field_id>_container">
<select id="field_<field_key>" name="item_meta[<field_id>]">
<option value=""> </option>
<option value="6">John Doe</option>
<option value="11">Jane Doe</option>
</select>
</div>
You’ll notice that option values are integer and not the actual names being displayed. This might be baffling when writing custom code to pull the user selected name from drop-down. However, these integer values are links back to the original form (FormA) entries driving the drop-down values. The numbers 6 & 11 (assumed for this example) are the corresponding item_ids for each entry from FormA.
So, while writing custom business logic to fetch the names from FormB, something as shown below will help. I am using hook frm_after_create_entry against FormB for illustration.
add_action('frm_after_create_entry', 'your_custom_method', 30, 2);
function your_custom_method($entry_id, $form_id) {
global $wpdb, $frm_entry, $frm_entry_meta;
if($form_id == 10) { //assuming FormB_id=10
//assuming Name field_id in FormA=111 & Name drop-down field_id in FormB=150
$name = $wpdb->get_var('SELECT meta_value FROM '.$wpdb->prefix.'frm_item_metas WHERE field_id=111 AND item_id='.$_POST['item_meta'][150]); //this line gets you the name from the original FormA entry based on the drop-down selection in FormB
//once user selected name is pulled from database, you are free to utilize the variable as per requirement
}
}
Hope this helps! Please leave a note and let me know if there is a better way to achieve the same result.