重力根据所选值的数量形成条件逻辑
Gravity forms conditional logic based on number of values selected
我有一个用重力表格构建的表格,它提出 5 个问题,每个问题有 2 个答案。
每个答案对应一个类别,因此我希望能够添加一些条件逻辑,以便根据哪个类别有更多答案将确认发送到不同的页面。
表格在这里:https://90daysfromretirement.com/medicare-advantage-vs-medicare-supplement/
这两个类别是“优势”和“补充”- 因此取决于答案,因此无论哪个类别有更多回复(3 个或更多),确认都会将它们重定向到适当的页面。
问题是我不知道如何计算每个值和 运行 逻辑。我认为我发现的任何重力形式附加组件都不可能,而且我无法将代码放在一起来实现它。
有什么建议吗?
因为你只有两个选项,所以你真的只需要知道一个的数量和问题的总数就可以知道哪个值被选中了。
比如你有10道题,“Advantage”选项被选了6次,你就知道Advantage比“Supplement”多了。
这里有一个片段可以帮助您入门。它将获取一组单选按钮字段,计算指定值被选中的次数,并将该计数填充到数字字段中。
然后您可以使用该 Number 字段对您的确认应用条件逻辑以重定向到适当的页面(例如,如果 Count 大于 5,则重定向到 Advantage 页面。如果 Count 小于 6,则重定向到 Supplement 页面).
/**
* Gravity Wiz // Gravity Forms // Value Counter
* https://gravitywiz.com/
*
* Count the number of times a given value has been selected in a group of fields and populate that number into a Number field.
* This snippet is designed to target a Number field and count selected values in Checkbox and Radio Button fields.
*
* This snippet works best with our free [GF Custom Javascript](https://gravitywiz.com/gravity-forms-custom-javascript/) plugin.
*/
// Replace the "1", "2" and "3" with field IDs of fields that should have their selected values counted. If you are using the
var $radios = jQuery( '#field_GFFORMID_1, #field_GFFORMID_2, #field_GFFORMID_3' );
// Replace "4" with the ID of the Number field in which the count should be populated.
var $target = jQuery( '#field_GFFORMID_4' );
// Replace "a" with the value you wish to count if selected.
var countValue = 'a';
function gwRecountValues() {
$target.find( 'input' ).val( $radios.find( 'input:checked[value="' + countValue + '"]' ).length );
}
$radios.on( 'change', function() {
gwRecountValues();
} );
gwRecountValues();
如果其他人使用它并遇到任何问题或需要改进,这里是将来可能会更新的来源:https://github.com/gravitywiz/snippet-library/commit/d34eb169da937981c4a1ea49bb8a36df023bff1b
此代码段中缺少的一件事是 PHP 验证。您可能不担心人们改变他们的计数,但没有 PHP 验证是可能的。
您可以尝试将以下内容添加到您的 functions.php 文件
add_action( "gform_after_submission_66", "after_submission", 10, 2 ); //replace 66 with your form number
function after_submission($entry, $form ){
$supplement_answers = array("milk", "apple", "pasta", "fries", "oats"); //replace array items with the supplement answers
$advantage_answers = array("red", "blue", "pink", "purple", "yellow"); //replace array items with the advantage answers
$field_nums = array("2","3","6","7","9"); //replace these field numbers with your own -- order then to align with answers above
$count_fields = count($field_nums);
$a = 0;
$b = 0;
for($i=0; $i<$count_fields; $i++){
if($entry[$field_nums[$i]] == $supplement_answers[$i]){
$a = $a + 1;
}else if($entry[$field_nums[$i]] == $advantage_answers[$i]){
$b = $b + 1;
}
}
if($a > $b){
header('Location: https://website/supplements_page'); //replace url
}else if($a < $b){
header('https://website/advantage_page'); //replace url -
}
}
我有一个用重力表格构建的表格,它提出 5 个问题,每个问题有 2 个答案。
每个答案对应一个类别,因此我希望能够添加一些条件逻辑,以便根据哪个类别有更多答案将确认发送到不同的页面。
表格在这里:https://90daysfromretirement.com/medicare-advantage-vs-medicare-supplement/
这两个类别是“优势”和“补充”- 因此取决于答案,因此无论哪个类别有更多回复(3 个或更多),确认都会将它们重定向到适当的页面。
问题是我不知道如何计算每个值和 运行 逻辑。我认为我发现的任何重力形式附加组件都不可能,而且我无法将代码放在一起来实现它。
有什么建议吗?
因为你只有两个选项,所以你真的只需要知道一个的数量和问题的总数就可以知道哪个值被选中了。
比如你有10道题,“Advantage”选项被选了6次,你就知道Advantage比“Supplement”多了。
这里有一个片段可以帮助您入门。它将获取一组单选按钮字段,计算指定值被选中的次数,并将该计数填充到数字字段中。
然后您可以使用该 Number 字段对您的确认应用条件逻辑以重定向到适当的页面(例如,如果 Count 大于 5,则重定向到 Advantage 页面。如果 Count 小于 6,则重定向到 Supplement 页面).
/**
* Gravity Wiz // Gravity Forms // Value Counter
* https://gravitywiz.com/
*
* Count the number of times a given value has been selected in a group of fields and populate that number into a Number field.
* This snippet is designed to target a Number field and count selected values in Checkbox and Radio Button fields.
*
* This snippet works best with our free [GF Custom Javascript](https://gravitywiz.com/gravity-forms-custom-javascript/) plugin.
*/
// Replace the "1", "2" and "3" with field IDs of fields that should have their selected values counted. If you are using the
var $radios = jQuery( '#field_GFFORMID_1, #field_GFFORMID_2, #field_GFFORMID_3' );
// Replace "4" with the ID of the Number field in which the count should be populated.
var $target = jQuery( '#field_GFFORMID_4' );
// Replace "a" with the value you wish to count if selected.
var countValue = 'a';
function gwRecountValues() {
$target.find( 'input' ).val( $radios.find( 'input:checked[value="' + countValue + '"]' ).length );
}
$radios.on( 'change', function() {
gwRecountValues();
} );
gwRecountValues();
如果其他人使用它并遇到任何问题或需要改进,这里是将来可能会更新的来源:https://github.com/gravitywiz/snippet-library/commit/d34eb169da937981c4a1ea49bb8a36df023bff1b
此代码段中缺少的一件事是 PHP 验证。您可能不担心人们改变他们的计数,但没有 PHP 验证是可能的。
您可以尝试将以下内容添加到您的 functions.php 文件
add_action( "gform_after_submission_66", "after_submission", 10, 2 ); //replace 66 with your form number
function after_submission($entry, $form ){
$supplement_answers = array("milk", "apple", "pasta", "fries", "oats"); //replace array items with the supplement answers
$advantage_answers = array("red", "blue", "pink", "purple", "yellow"); //replace array items with the advantage answers
$field_nums = array("2","3","6","7","9"); //replace these field numbers with your own -- order then to align with answers above
$count_fields = count($field_nums);
$a = 0;
$b = 0;
for($i=0; $i<$count_fields; $i++){
if($entry[$field_nums[$i]] == $supplement_answers[$i]){
$a = $a + 1;
}else if($entry[$field_nums[$i]] == $advantage_answers[$i]){
$b = $b + 1;
}
}
if($a > $b){
header('Location: https://website/supplements_page'); //replace url
}else if($a < $b){
header('https://website/advantage_page'); //replace url -
}
}