Forums

how to count the number of checkboxes checked

vampmaster 05 Aug, 2015
Hi I'm trying to do a quiz, I need the form to add all the values of the checkboxes (each checked checkbox equals to 1) so I know what the sumatory of check marks I have in my form. Then I need to get the score and post it in the result page, this is the page to get after submiting the form. I also need the email that is send to the quizer to have the sumatory of check marks.

Can anyone point me into how to do this?
GreyHead 06 Aug, 2015
Hi vampmaster,

You can do this with PHP in a Custom Code action after the form submits. Is this one single checkbox group - or a lot of separate single checkboxes?

Bob
vampmaster 06 Aug, 2015
A lot of single separated checkboxes. I can assign checkbox A with a check value 1, unchecked value of 0, checkbox B chk=1 unchk=0...checkbox T chk= 1 unchk=0.
$A=$_post['A'];
$B=$_post['B'];  
....
$T=$_post['T'];
$grade=$A+$B...+$T;


Now in the last page I would put Echo $grade;
Now where do I put and how to I put this code in the form and how do I tell the form to execute them. How do I tell the thank you form to put the Echo grade.
And 3. How do I send the grade by email {grade} to call the grade variable.
Thank you very much Bob.
GreyHead 07 Aug, 2015
Hi vampmaster,

The simplest solution is to give all the checkboxes the same array name e.g. question[] Then, in the On Submit event you can count them in a custom code action like this
<?php
$form->data['score'] = count($form->data['question']);
?>

If they have to have different names then you can use custom code like this
<?php
$questions = array(
  'A', 'B', . . . 'T'
);
$count = 0;
foreach ( $questions as $q ) {
  if ( !empty($form->data[$q]) ) {
    $count ++;
  } 
}
$form->data['score'] = $count;
?>
In both cases you can then use {score} to show the score.

Bob
vampmaster 07 Aug, 2015
Hi Bob, I have the code as follows:
<div class="form-group gcore-form-row" id="form-row-Nombre"><label for="Nombre" class="control-label gcore-label-left">Nombre</label>
<div class="gcore-input gcore-display-table" id="fin-Nombre"><input name="Nombre" id="Nombre" value="" placeholder="" class="form-control A" title="" style="" data-inputmask="" data-load-state="" data-tooltip="" type="text" /></div></div><div class="form-group gcore-form-row" id="form-row-Correo"><label for="Correo" class="control-label gcore-label-left">Correo electrónico</label>
<div class="gcore-input gcore-display-table" id="fin-Correo"><input name="Correo" id="Correo" value="" placeholder="" class="form-control A" title="" style="" data-inputmask="" data-load-state="" data-tooltip="" type="text" /></div></div><div class="form-group gcore-form-row" id="form-row-Pais"><label for="Pais" class="control-label gcore-label-left">País</label>
<div class="gcore-input gcore-display-table" id="fin-Pais"><input name="Pais" id="Pais" value="" placeholder="" class="form-control A" title="" style="" data-inputmask="" data-load-state="" data-tooltip="" type="text" /></div></div><div class="form-group gcore-form-row" id="form-row-captcha"><div class="gcore-subinput-container-wide" id="fitem"><label for="captcha1" class="control-label gcore-label-left">Captcha</label>
<div class="gcore-input pull-left gcore-sub-input gcore-display-table" id="fin-captcha1"><input placeholder="" class="form-control A" title="" style="" type="text" name="captcha" id="captcha1" /></div></div>
<div class="gcore-subinput-container-wide" id="fitem1"><div class="gcore-input pull-left gcore-sub-input gcore-display-table" id="fin-captcha2">{captcha_img}</div></div></div><div class="form-group gcore-form-row" id="form-row-button25"><div class="gcore-input gcore-display-table" id="fin-button25"><input name="button25" id="button25" type="submit" value="Enviar" class="btn btn-default form-control A" style="" data-load-state="" /></div></div>
Chronoforms says I have 16 errors and I can no longer see the checkboxes in the form:
http://ondakids.com/index.php/para-papa/50-test-para-saber-si-eres-papa-helicoptero-o-no

Also I'm going to try to save the name, email, country, and score to the database with the DB Save comand.
1. I wonder where the 16 errors are, what happen to my checkboxes and how to name each checkbox question since it appears it has ID's and I understand there can only be one ID name per form.
2. How to tell the form just to save the fields with the DB Save comand.
3. Do I put the code you suggested on submit event field or how do I create the event, or it's better to say where do I create the event.

Sorry for so many questions.
GreyHead 08 Aug, 2015
Hi vampmaster,

You can't see the checkboxes because there are none in the form HTML you posted. I can't tell why that would be.

You can give the checkboxes all the same array name e.g. question[] but separate ids question_a, question_b, . . .

I can't tell what the 16 errors are, the details are only visible if you use the 'Advanced Wizard' :-(

The code I suggested goes in a Custom Code action in the form On Submit event.

I don't understand your q2 about the DB Save??

Bob
vampmaster 10 Aug, 2015
Thanks Bob,
1. We changed all the fields name from checkbox1, checkbox2, ..., checkbox20 to question['01'], question['02'], ..., question['20']. And all the checkboxes reappeared and I guess all the fields are an array now. Thanks for that.

2. Then we went to the events section and tryed to put the custom code
<?php
$form->data['score'] = count($form->data['question']);
?>
but I don't see an event called custom code, in which part do we to put the code? I'm assuming it's an event.

3. To show the score in the results page do I use an echo {score}; ?
vampmaster 10 Aug, 2015
Hi Bob,

As they say in pool all was in the wrist movement. Once we were able to load the form in the advance interface we could add the custom code, change the display message and finally everything is ready to test. I'll keep you posted. No more doubts for the moment.
Thanks a zillion.
This topic is locked and no more replies can be posted.