Hi Chris,
I wound starting over with this and now it works like a top. I'll include the code so other people can use the the solution I worked out last night.
As a summary, I'm using the gform_field_value_$parameter_name filter as you suggested. The documentation is here:
http://www.gravityhelp.com/documentation/page/Gform_field_value_$parameter_name
I'm doing custom MySQL queries to the wp_rg_lead_detail table to get data out of a form that's been previously submitted (Form 1), then populating that data into a new form (Form 2).
The following code pulls the cell phone number out of Form 1, and dynamically populated the cell phone input box in Form 2.
Key things to remember:
1) you must change the following code to reflect your form number.
2) you must enable the "Allow field to be populated dynamically" under the Admin tab on the form input you want to populate.
3) you must use "cellphone" (with out quotes) as the dynamic population parameter.
<?php
add_filter("gform_field_value_cellphone", "populate_cellphone");
function populate_cellphone($value) {
global $wpdb;
// Change this to be the number of the form you're using
$form_id = 1;
// Do a direct MySQL lookup based on the entry_id you need
$sql = "
SELECT
ld.lead_id,
ld.field_number,
ld.value
FROM
wp_rg_lead_detail ld
INNER JOIN wp_rg_lead l ON ld.lead_id = l.id
WHERE
ld.form_id = %d AND lead_id = '".$_REQUEST['entry_id']."'
ORDER BY
ld.lead_id,
ld.field_number
";
$sql = $wpdb->prepare($sql,$form_id);
$results = $wpdb->get_results($sql, ARRAY_A);
$form_answers = array();
foreach($results as $row) {
$row = (object)$row;
$major_num = (int)$row->field_number;
$minor_num = (int)(($row->field_number-$major_num)*10);
$form_answers[$row->lead_id][$major_num][$minor_num] = $row->value;
}
// Iterate through the array of data pulled from the dB
foreach($form_answers as $i => $lead) {
foreach($lead as $j => $form_answer) {
$form_answers[$i][$j] = implode(' ',$form_answer);
}
}
// DUMP the variables to see what's in the array
// Uncomment the line containing print_r below to see everything
// that's pulled from the MySQL Query. This will show you the correct
// position for the data you're looking for
//echo '<pre>'; print_r($form_answers); echo '</pre>';
$my_lead_id = $_REQUEST['entry_id'];
// Return the requested variable - note, 14 is the position of the
// cell phone number in the array of data pulled from the dB
return $form_answers[$my_lead_id]['14'];
}
?>
Posted 11 years ago on Thursday December 27, 2012 |
Permalink