Calculate the number of As, Bs, Cs, etc selected to give a total [RESOLVED]

hi there,
I have a form which asks users to select an answer of A = Always | B = Often | C = Sometimes | D = Rarely | E = Never

I would like to find a way to calculate the number of As, Bs and Cs etc selected which will inform next steps.

here’s the form https://louiseselbydyslexia.com/visual-skills-questionnaire/

Any help gratefully received :slightly_smiling_face:
Thank you!

Just posting here again in case this request has slipped through the net. Would love to get some help on this one. Thanks!

Hi Gaby. You can do this with Gravity Forms. The approach will be to use the gform_pre_submission filter to count up the number of times each answer was selected, and store that in three variables (in your case maybe $a, $b and $c). Then, you populate another field in the form with that number, and you can then use the number of A, B or C in conditional logic for the confirmation or notification or conditional shortcodes.

Here is the documentation for the gform_pre_submission filter:

With that, you will be looping through the $_POST to count the number of times each A, B and C was selected, and incrementing that counter. Then, at the end, assign those values back to three hidden fields in the form.

It will require some simple PHP.

There used to be a plugin which did this, but it’s out of date and I don’t know if it continues to work:

1 Like

Hi Chris,

Thanks so much for your response.

It’s the “simple php” I think I need the help with, if at all possible?

Thank you!
Gaby

Gaby Prothero
gabyprotherodesign.com

Hi Gaby. I worked up a form and some code for you. This is how you can do it, but this should be considered a starting point or example, not a full solution.

You can download the form from here:
[SITE REMOVED]

Save that json file to your computer, then import it on your site using the Forms > Import/Export > Import Forms functionality. Once imported, click the link to edit the form, and make a note of the form ID. You will need that for the next step.

You can download the PHP code from here: Count frequency of submitted radio buttons - Droplr

That is PHP code which can be added to your theme functions.php file, or you can use a plugin such as this one to keep the code separate from the theme: Code Snippets – WordPress plugin | WordPress.org

If you are using the code snippets plugin, you can import that code from this file:

[SITE REMOVED]

(the Code Snippets plugin has an import feature)

Once you add the code to the site, the only change required is the form ID. It is 229 in my code. Change that to the ID of the form you previously imported.

Then, preview the form. Once submitted, check to see if the last five fields in the form are populated with the counts of the choices you made.

With those five counts fields (one for each possible answer) in the entry, you can use conditional logic on the confirmations and notifications, and can use those fields as comparisons in the conditional shortcode as well: Conditional Shortcode - Gravity Forms Documentation

Please give that a try and let me know if I left anything out, or if you have any questions for me. Thank you.

Hi Chris

Thank you SO MUCH for all your help with this. It’s all worked perfectly my end and I have created a new form functioning just as I wanted it to.

In case you’re interested here it is https://louiseselbydyslexia.com/visual-skills-questionnaire-2/

I really appreciate your assistance on this one.

Best wishes
Gaby

Gaby Prothero
gabyprotherodesign.com

1 Like