Forums

Sum checkboxes values after submitting and generate corresponding message

petrn 10 Jan, 2019
Hello, I would like to create a questionnaire consisting of 6 checkboxes (1 max choice) with answers and their respective values as below,

1=Strongly disagree
2=Disagree
3=Neither agree nor disagree
4=Agree
5=Strongly Agree

When submitted the questionnaire will sum the values (points) and then generate one of the 3 messages, first for a sum of points in a range from 0 to 10, second in a range from 11 to 20 and third in a range from 21 to 30.

Currently I am lost and not really sure how to do that, appreciating any help. Thank you in advance.

Petr
GreyHead 11 Jan, 2019
Hi Petr,

Sorry but I don't understand your form setup - is this six sets of radio buttons?

Basically you can use an Event Switcher in the On Submit event to sum the results and display the corresponding message.

Bob
petrn 11 Jan, 2019
Sorry, I was meaning 6 questions, and each question has 5 checkboxes with the answers and their respective values as stated above, see image.

I knew that the Event Switcher action is a potential option, however, I am not sure what exactly to put in the data provider and events to generate one of the three corresponding messages.

I am not an experienced programmer and thus I cannot solve this that easily.

Petr
​[file=11391]questionnaire.png[/file]
healyhatman 11 Jan, 2019
{data.sum:questions}

Call your fields questions[#] replacing the # with either a number or unique name
petrn 11 Jan, 2019
Thank you, that worked very well! Although now that I have the sum of the values, how to implement the number range in the event switcher?

Petr
healyhatman 11 Jan, 2019
Ok use a PHP block instead, array sum $this->data($questions). Use if / then / else, return some value and use {var:phpblockname} in your event switch data source.
petrn 16 Jan, 2019
Could you please provide an example of the code before the 'if' function? I tried many times but always ended with error, otherwise returning the assigned value for the event switcher won't be a problem.
healyhatman 16 Jan, 2019
Answer
$total = array_sum($this->data("questions"));

if($total < 20) { return 1; }
else if($total < 40) { return 2; }
else if($total < 50) { return 3; }
petrn 17 Jan, 2019
Thank you for your fast reply! This is what I have had already tried for the firs time but it still ended with error. Any other possible suggestions?
healyhatman 17 Jan, 2019
I suggest you be clearer about what you mean by "error"
petrn 17 Jan, 2019
After submitting the form returns

0 syntax error, unexpected 'if' (T_IF)


even without showing the debug.
healyhatman 17 Jan, 2019
Did you copy and paste from the forum? If so delete Everything and type out manualy.
petrn 17 Jan, 2019
The form does not end with error now, but it doesn't sum correctly, I used this code

$total = array_sum($this->data($questions));

if($total >= 0 && $total <= 10) { return "1"; }
elseif ($total >= 11 && $total <=20) { return "2"; }
elseif ($total >= 21 && $total <=30) { return "3"; }


and it always keep returning var=1 even when the sum of values is for example 24.
petrn 17 Jan, 2019
Array
(
    [option] => com_chronoforms6
    [cont] => manager
    [chronoform] => o3-questionnaire
    [event] => submit
    [questions] => Array
        (
            [1] => 4
            [2] => 4
            [3] => 4
            [4] => 4
            [5] => 4
            [6] => 4
        )

    [button16] => 
)
Array
(
    [data] => Array
        (
            [returned] => 1
            [var] => 1
        )

)
healyhatman 17 Jan, 2019
Are you using the correct PHP block, or the incorrect custom code block?
petrn 17 Jan, 2019
Yes, I use the PHP block, here is a pic of the functions setup.[file=11432]setup.png[/file]
petrn 17 Jan, 2019
1 Likes
Okay, I sorted it out. I falsely called the array in the array_sum. It is working now. Thank you for your help!
healyhatman 17 Jan, 2019
What do the settings of the Event Switcher look like
healyhatman 17 Jan, 2019
Coolbeans. Mark http://www.chronoengine.com/forums/posts/t106576/p382910#p382910 as the answer if you can thanks, or pick another one as the answer.
This topic is locked and no more replies can be posted.