Populating hidden field based on comparison of field values

I’d like to compare the values in Fields 121, 122, and 123, and then display the result in a hidden field in the form confirmation. At the moment this triggers a critical error. Grateful for any suggestions!

add_filter("gform_pre_submission_9", "find_highest_percent", 10, 4);
function find_highest_percent( $a, $b, $c, $entry, $form ) 
	{
	$a = rgar( $entry, '121' ) ; 
	$b = rgar( $entry, '122' ) ; 
	$c = rgar( $entry, '123' ) ;
	
	$total = $a + $b + $c;
	$a_percent = ($a / $total) * 100;
	$b_percent = ($b / $total) * 100;
	$c_percent = ($c / $total) * 100;
	
	if (abs($a_percent - $c_percent) <= 10) {
		$result = "AC";
	} elseif (abs($a_percent - $b_percent) <= 10) {
		$result = "AB";
	} elseif (abs($b_percent - $c_percent) <= 10) {
		$result = "BC";
	} elseif (abs($a_percent - $b_percent) <= 10 && abs($a_percent - $c_percent) <= 10 && abs($b_percent - $c) <= 10) {
		$result = "ABC";
	} else {
		if ($a_percent > $b_percent && $a_percent > $c_percent) {
			$result = "A";
		} elseif ($b_percent > $a_percent && $b_percent > $c_percent) {
			$result = "B";
		} else {
			$result = "C";
		}
	}
	$_POST["input_126"] = $result;
}

There are quite a few problems with this code.

  1. The gform_pre_submission filter has just one argument. This is what your first two lines of code should look like:
add_filter("gform_pre_submission_9", "find_highest_percent");
function find_highest_percent($form ) {
  1. There is no $entry available yet, so you can’t get your $a, $b and $c values from that. You would need to get them from the post instead, like this:
$a = rgpost( 'input_121' ) ; 
$b = rgpost( 'input_122' ) ; 
$c = rgpost( 'input_123' ) ;

That should take care of the fatal errors I can see at first glance. If more occur, please share the actual error message, and we’ll continue to review. We have not reviewed your approach or the code you use to compare a, b, and c with your total and percentages. We’re first looking at using the filter correctly so it does not error out.

Thanks so much for your help Chris. That makes sense and does take care of the critical error.

My form is a questionnaire with three categories (a, b, and c). Each question has a value of ‘1’. Fields 121, 122, and 123 are number fields that calculate the number of selected answers in each category.

At the moment it seems this function is not receiving any values from the form. To test I’ve simplified the code and have added hidden fields (127, 128, and 129) to my form that would show the values in my notification. Currently these fields are blank.

add_action("gform_pre_submission_9", "find_highest_percent");
function find_highest_percent( $form ) 
	{
	$a = rgpost( 'input_121' );
	$b = rgpost( 'input_122' );
	$c = rgpost( 'input_123' );

        $_POST["input_127"] = $a;
	$_POST["input_129"] = $b;
	$_POST["input_128"] = $c;
}

Try adding some logging statements to the code. First, enable Gravity Forms logging: https://docs.gravityforms.com/logging-and-debugging/#h-enabling-logging

Then, try this updated code:

add_action("gform_pre_submission_9", "find_highest_percent");
function find_highest_percent( $form ) 
	{
	// are we running
	GFCommon::log_debug( __METHOD__ . '(): running for form 9.' );
	// log the $_POST
	GFCommon::log_debug( __METHOD__ . '(): THE POST => ' . print_r( $_POST, true ) );
	
	$a = rgpost( 'input_121' );
	$b = rgpost( 'input_122' );
	$c = rgpost( 'input_123' );

	$_POST["input_127"] = $a;
	$_POST["input_129"] = $b;
	$_POST["input_128"] = $c;
}

Finally, check the Gravity Forms Core log. You will find the link on the Forms → Settings → Logging page. Share the link to the log file if you need assistance interpreting the logs. Thank you.

Thanks Chris. I enabled logging but wasn’t able to interpret them well enough to see where the issue might be. The log is at https://lifespadev-blog.sitedistrict.com/wp-content/uploads/gravity_forms/logs/gravityforms_83ba881854950f092c5beed1af29a543a213b6dd.txt

The variables $a, $b, and $c refer to the three categories of vata, pitta, and kapha in the questionnaire. Thanks again. Please let me know if there’s any other info I can share that would be helpful.

In the log, input_121, input_122, and input_123 do not exist in the $_POST. If you search the log for any of those values, you won’t find them. This is the _POST that was logged:

[input_77_1] => 1
    [input_77_2] => 1
    [input_77_3] => 1
    [input_77_4] => 1
    [input_77_5] => 1
    [input_77_6] => 1
    [input_77_7] => 1
    [input_77_8] => 1
    [input_77_9] => 1
    [input_80_1] => 1
    [input_80_2] => 1
    [input_80_3] => 1
    [input_80_4] => 1
    [input_80_5] => 1
    [input_80_6] => 1
    [input_80_7] => 1
    [input_80_8] => 1
    [input_80_9] => 1
    [input_80_11] => 1
    [input_80_12] => 1
    [input_80_13] => 1
    [input_84_1] => 1
    [input_84_2] => 1
    [input_84_3] => 1
    [input_84_4] => 1
    [input_84_5] => 1
    [input_84_6] => 1
    [input_84_7] => 1
    [input_84_8] => 1
    [input_84_9] => 1
    [input_88_1] => 1
    [input_88_2] => 1
    [input_88_3] => 1
    [input_88_4] => 1
    [input_88_5] => 1
    [input_88_6] => 1
    [input_88_7] => 1
    [input_88_8] => 1
    [input_89_9] => 1
    [input_89_11] => 1
    [input_89_12] => 1
    [input_91_8] => 1
    [input_91_9] => 1
    [input_91_11] => 1
    [input_92_1] => 1
    [input_92_2] => 1
    [input_93_3] => 1
    [input_93_4] => 1
    [input_93_5] => 1
    [input_93_6] => 1
    [input_93_7] => 1
    [input_72_3] => test
    [input_72_6] => test
    [input_69] => test@test.com
    [input_71_1] => I agree to LifeSpa\'s <a href=\"/privacy-policy/\">privacy policy</a> and <a href=\"/terms-of-use\">terms of use</a>
    [input_127] => 
    [input_129] => 
    [input_128] => 
    [input_126] => 
    [input_130] => 

Double-check to be sure those are the correct input IDs from the form.

Chris, thank you so much for looking at this. Yes those are the correct field IDs. They are number fields that calculate the values of the selected responses in each category (all set to ‘1’). I am able to display their values in my notification using {Vata Total:121:value} but when I pull it into my custom function using $a = rgpost( ‘input_121’ ); and then try to display the value in a hidden field on my notification using $_POST[“input_127”] = $a nothing populates.

Chris, just wanted to follow up. Those should be the correct IDs. Would there be any other reason those wouldn’t be populating? Thanks again!

It sounds like the values are not being calculated before that page of the form is posted, which is why they are not available in the $_POST. If you check the logging, you can see they are not there. That means they’re being calculated after the page is submitted, or they are hidden when that page is submitted. Hidden fields are not included in the $_POST.

Thanks so much Chris. I suspected that might be the case - the values are not being calculated before that page is posted. Currently the calculations are done within the number fields (e.x. {Vata Total:121:value}). Is it possible to calculate the values of the checkbox fields in my function instead?

Thanks again Chris! Since each option in the checkbox field has a value of 1 would it be possible to calculate the totals programmatically and then return the totals to a hidden field or a numeric field? I’m hoping I could use that total to determine which notification to send the participant.

Hi Kumara. You can’t use the same value of 1 for all options in the checkbox field. If you need them to add up as one, but still be unique, use values like this:

  • 1.00001
  • 1.00075
  • 1.0009
  • 1.00005
  • 1.00002

When added together and rounded to zero decimal places, those will add up as one. And they are unique so the proper selected checkboxes will be stored in the entry.

If you have any other questions, please let us know. Thank you.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.