We have a simple 5-question quiz, each with four possible answers, A-B-C-D (radio buttons). All I need to do is populate four fields with how many of each were selected.
I’ve set up my form with the 5 radio button questions and then 4 hidden text fields. Using what I could find in the gform_pre_submission documentation + code I pulled from a few other threads on here with similar requests, I have the following code, but it is not working. The 4 text fields do not populate with anything.
This is almost verbatim what another said code straight from Gravity worked in another thread. What am I missing? Thanks!
// 34 = quiz form ID
add_action( 'gform_pre_submission_34', function ( $form ) {
GFCommon::log_debug( __METHOD__ . '(): The POST => ' . print_r( $_POST, true ) );
// ID of the first radio button field that will be counted
$first_field = 1;
// ID of the last radio button field that will be counted
$last_field = 5;
// initialize an empty array to hold all our radio button fields
$radio_fields = array();
// loop through all the radio button fields and assign them to an array
for ( $i = $first_field; $i <= $last_field; $i++ ) {
GFCommon::log_debug( __METHOD__ . "(): Counter is {$i}." );
$radio_fields[] = rgpost ( 'input_' . $i );
}
$counts = array_count_values ( $radio_fields );
GFCommon::log_debug( __METHOD__ . '(): COUNTS => ' . print_r( $counts, true ) );
// store those counts in five different fields in the form - field IDs 7 thru 10
$_POST['input_6'] = $counts['How Many As'];
$_POST['input_7'] = $counts['How Many Bs'];
$_POST['input_8'] = $counts['How Many Cs'];
$_POST['input_9'] = $counts['How Many Ds'];
GFCommon::log_debug( __METHOD__ . '(): MODIFIED POST => ' . print_r( $_POST, true ) );
} );