Count results of selected radio buttons

Hi all,

I’m building a form that uses the same set of radio button answers for each question. When the form is submitted, I need to publish the total number of each type of answer on the confirmation page.

On top of that, I need to show a sum of the count for the first 2 types of Answers.

So something like:
You chose Answer A & B 10 times (Answer A 4 times, Answer B 6 times)
You chose Answer C 4 times
You chose Answer D 1 time

Is there a way to get the total count and use Merge Tags (using the Gravity Wiz perk Merge Tags perk) on the confirmation page?

Thanks in advance for any tips.

I don’t know of a way to do that with Gravity Wiz, but I will tag them and hope they show up with something.

In general, you would need to use PHP to count the frequency that each option was selected, and then store that in a field in the form, so you can use that number on your confirmation page. I usually do this with the gform_pre_submission filter:

Thanks Chris, I’ll take a look at that.

Seems that if I’m creating fields to store counts, I could create 1 to store the totals of the 1st 2 answers and use that for a merge tag.

Yes, exactly. If you need help getting started with the code, let us know.

Thanks Chris, appreciate your help.

Back again … Populating the new fields is working just fine using gform_pre_submission.

What I can’t wrap my head around is how to take those posted values and use them to count the frequency.

Can you point me in the right direction?

What code are you using to populate the fields? I’m curious what you ware populating them with if not the frequency? Can you share the code here?

Hi Chris,

Here’s what I’ve set up in my functions.php to populate the fields (there will eventually be 35 fields like this):

	// $_POST is the visible question, rgpost holds the answer for counting
	function pre_submission_handler( $form ) {
		$_POST['input_21'] = rgpost( 'input_18' );
		$_POST['input_22'] = rgpost( 'input_20' );
	}
	add_action( 'gform_pre_submission_1', 'pre_submission_handler' );

If I want to use something like array_count_values($array), how do I get that array from each entry? I’ve gone through your docs and I’m just not finding that piece of the puzzle.

Does that make more sense?

I’ve added this code to my Results template and I’m getting odd results.


				$args_rgpost = array(
					'a1' => rgpost( 'input_21' ),
					'a2' => rgpost( 'input_22' )
				);
				print_r($args_rgpost);
				print_r(array_count_values($args_rgpost));


				$args_rgar = array(
					'a1' => rgar( $entry, '21' ),
					'a2' => rgar( $entry, '22' )
				);
				print_r($args_rgar);
				print_r(array_count_values($args_rgar));

With this output …

rgpost:Array
(
    [a1] => 
    [a2] => 
)
Array
(
    [] => 2
)
rgar:Array
(
    [a1] => 
    [a2] => 
)
Array
(
)

Creating the array using values from rgpost seems to be headed in the right direction but the empty values is a bit confusing. The inputs I’m storing the answers in are set us Number fields.

I’m assuming that if I can get the actual values in to the rgpost array, the rest should all fall in to place. Am I on the right track?

© 2008 - 2021. Gravity Forms is a project by Rocketgenius Inc.