This topic is locked

Can you use Check boxes when calculating on the fly?

7/28/2011 5:43:57 PM
PHPRunner General questions
author

I'm trying to calculate on the fly using code below. I'm using checkboxes that have the following values:
Exceeds = 5

Proficient = 3

Below = 1

Unsatisfactory = 0
Can I use this code for this task? Or is there some other way to do this. Thanks for any help!!

var ctrlEngaged = Runner.getControl(pageid, 'oneEngaged');

var ctrlSuccessful = Runner.getControl(pageid, 'oneSuccessful');

var ctrloneScore = Runner.getControl(pageid, 'oneScore');
function func() {

ctrloneScore.setValue(parseFloat(ctrlEngaged.getValue()) + parseFloat(ctrlSuccessful.getValue()));

};
ctrlEngaged.on('keyup', func);

ctrlSuccessful .on('keyup', func);
C
cgphp 7/28/2011

Please could you more clear ? keyup is not a valid event for checkboxes.

501375 7/28/2011

Thanks for the reply!
Sorry for not being clear..I hope this helps explain it better,
I'm trying to calculate check box fields. Each question has the following checkbox choices:
Exceeds

Proficient

Below

Unsatisfactory
Each one of the choices has a certain value or weight to them when saved the value is entered into the db.
Exceeds = 5

Proficient = 3

Below = 1

Unsatisfactory = 0
How can I calculate the check box choices to be totaled in a Total field.

C
cgphp 7/28/2011
var ctrlTotal = Runner.getControl(pageid,'total_field_name');

var total_temp;

ctrlTotal.setValue(0);
$("input[type='checkbox'][id='checkbox_id']").click(function(e){

total_temp = ctrlTotal.getValue();

if($(this).is(':checked'))

ctrlTotal.setValue(parseInt($(this).val()) + parseInt(total_temp));

else

ctrlTotal.setValue(parseInt(parseInt(total_temp) - parseInt($(this).val())));

});
501376 7/28/2011

Again thanks for helping me out. I tried the code and it didn't work for me. It did place a zero in the total field but it is not getting the value from the check boxes. Here is what I'm using:

var ctrlTotal = Runner.getControl(pageid,'oneScore');

var total_temp;

ctrlTotal.setValue(0);
$("input[type='checkbox'][id='checkbox_id']").click(function(e){

total_temp = ctrlTotal.getValue();

if($(this).is(':checked'))

ctrlTotal.setValue(parseInt($(this).val()) + parseInt(total_temp));

else

ctrlTotal.setValue(parseInt(parseInt(total_temp) - parseInt($(this).val())));

});


I only changed the total field name..Am I suppose to change something else in the code that you sent me. Also, the checkboxes are using a lookup wizard. Does that complicate the situation??
Again thank you! You have helped me a couple of times and I am really appreciative of it!!


var ctrlTotal = Runner.getControl(pageid,'total_field_name');

var total_temp;

ctrlTotal.setValue(0);
$("input[type='checkbox'][id='checkbox_id']").click(function(e){

total_temp = ctrlTotal.getValue();

if($(this).is(':checked'))

ctrlTotal.setValue(parseInt($(this).val()) + parseInt(total_temp));

else

ctrlTotal.setValue(parseInt(parseInt(total_temp) - parseInt($(this).val())));

});


C
cgphp 7/29/2011

What's the name of the lookup ?

C
cgphp 7/29/2011
var ctrlTotal = Runner.getControl(pageid,'total_field_name');

var total_temp;

ctrlTotal.setValue(0);
$("input[type='checkbox'][id^='value_name_of_lookup']").click(function(e){

total_temp = ctrlTotal.getValue();

if($(this).is(':checked'))

ctrlTotal.setValue(parseInt($(this).val()) + parseInt(total_temp));

else

ctrlTotal.setValue(parseInt(parseInt(total_temp) - parseInt($(this).val())));

});


Change "name_oflookup" with yours keeping "value" before.

501377 7/29/2011

Thanks again,
I have tried the following without any luck:

var ctrlTotal = Runner.getControl(pageid,'oneScore');

var total_temp;

ctrlTotal.setValue(0);
$("input[type='checkbox'][id^='value_oneEngaged_1[]']").click(function(e){

total_temp = ctrlTotal.getValue();

if($(this).is(':checked'))

ctrlTotal.setValue(parseInt($(this).val()) + parseInt(total_temp));

else

ctrlTotal.setValue(parseInt(parseInt(total_temp) - parseInt($(this).val())));

});


Here is the information from FireBug. I have tried both the Input ID and the Name.

<input id="value_oneEngaged_1_0" type="checkbox" value="5" name="value_oneEngaged_1[]">

<b id="data_value_oneEngaged_1_0">Exceeds</b>
<input id="value_oneEngaged_1_1" type="checkbox" checked="checked" value="3" name="value_oneEngaged_1[]">

<b id="data_value_oneEngaged_1_1">Proficient</b>
<input id="value_oneEngaged_1_2" type="checkbox" value="1" name="value_oneEngaged_1[]">

<b id="data_value_oneEngaged_1_2">Below</b>
<input id="value_oneEngaged_1_3" type="checkbox" value="0" name="value_oneEngaged_1[]">

<b id="data_value_oneEngaged_1_3">Unsatisfactory</b>
C
cgphp 7/29/2011

Try this:



var ctrlTotal = Runner.getControl(pageid,'oneScore');

var total_temp;

ctrlTotal.setValue(0);
$("input[type='checkbox'][id^='value_oneEngaged']").click(function(e){

total_temp = ctrlTotal.getValue();

if($(this).is(':checked'))

ctrlTotal.setValue(parseInt($(this).val()) + parseInt(total_temp));

else

ctrlTotal.setValue(parseInt(parseInt(total_temp) - parseInt($(this).val())));

});
501378 7/29/2011

Thanks im getting values now in the total field but.. it is not giving the the correct values. Example:
When selecting "Exceeds" it should be giving the value of 5. It is giving me a value of 2.
Proficient = 3 it is giving the value 0

Below = 1 it is giving the value -2

Unsatisfactory = 0 it is giving the value -3
Thanks for any help/tips you can give me!!



Try this:



var ctrlTotal = Runner.getControl(pageid,'oneScore');

var total_temp;

ctrlTotal.setValue(0);
$("input[type='checkbox'][id^='value_oneEngaged']").click(function(e){

total_temp = ctrlTotal.getValue();

if($(this).is(':checked'))

ctrlTotal.setValue(parseInt($(this).val()) + parseInt(total_temp));

else

ctrlTotal.setValue(parseInt(parseInt(total_temp) - parseInt($(this).val())));

});


C
cgphp 7/29/2011

Please, post the full code of your Javascript Onload event.

501379 7/29/2011

Here you go..Thank you so much for the help. We are so close!!!

var ctrlTotal = Runner.getControl(pageid,'oneScore');

var total_temp;

ctrlTotal.setValue(0);
$("input[type='checkbox'][id^='value_oneEngaged']").click(function(e){

total_temp = ctrlTotal.getValue();

if($(this).is(':checked'))

ctrlTotal.setValue(parseInt($(this).val()) + parseInt(total_temp));

else

ctrlTotal.setValue(parseInt(parseInt(total_temp) - parseInt($(this).val())));

});
C
cgphp 7/29/2011

Check firebug for some errors or warnings.

501380 7/29/2011

Thanks, I will do that!

C
cgphp 7/29/2011

Or post the link of a demo.

501381 7/29/2011



Check firebug for some errors or warnings.


cgphp, I have it working now. forgot to mention that it is defaulted to value of 3. Once I changed

ctrlTotal.setValue(0);

to

ctrlTotal.setValue(3);

it worked.
OK, so now is my chance to ask you another question pertaining to this.
In this particular section (Domain I)
I have 5 questions each with the same checkboxes mentioned in this thread. Using the working script How would I add the next field checkbox selection to total in the total field.
Man, hope I'm asking that correctly. <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=59804&image=1&table=forumreplies' class='bbc_emoticon' alt=':)' />
Once again thank you!

C
cgphp 7/29/2011

Inside the click event:

$(this).next('input').val();
501382 7/29/2011

I'm not sure I know how to put that in the script. Here is what I did but it didn't do anything. <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=59811&image=1&table=forumreplies' class='bbc_emoticon' alt=':(' />
My next field name is value_oneSuccessful.

var ctrlTotal = Runner.getControl(pageid,'oneScore');

var total_temp;

ctrlTotal.setValue(3);
$("input[type='checkbox'][id^='value_oneEngaged']").click(function(e){

$(this).next('input').val();

total_temp = ctrlTotal.getValue();

if($(this).is(':checked'))

ctrlTotal.setValue(parseInt($(this).val()) + parseInt(total_temp));

else

ctrlTotal.setValue(parseInt(parseInt(total_temp) - parseInt($(this).val())));

});




Inside the click event:

$(this).next('input').val();


C
cgphp 7/29/2011

To provide you the right code I need to know exactly what you want to achieve.

C
cgphp 7/29/2011

Please, post a screenshot of your actual form and specify the fields name.

501383 7/29/2011



To provide you the right code I need to know exactly what you want to achieve.


cgphp...I understand and I'm sorry I'm not very clear on my question. Here goes another try..
On the add form I have the following:
[indent]Field 1 - 4 check boxes
Field 2 - 4 Check Boxes
Field 3 - 4 check boxes
Field 4 - 4 Check Boxes
Field 5 - 4 Check Boxes
Field 7 - Comments Box

Field 8 - Comments Box

Field 9 - Comments Box
Field 10 - Total Box[/indent]
What I need to do is total all the check box field selections in the "Field 10" Total Box.
The code is working great for the "Field 1" but I'm not sure how to get the value for the remainder check boxes to total in "Field 10"
Again, sorry about not explaining this clearly!

C
cgphp 7/29/2011

Please, specify exactly the fields name.

501384 7/29/2011



Please, specify exactly the fields name.


Field 1 is oneEngaged

Field 2 is oneSuccessful

Field 3 is oneCritical

Field 4 is oneSelf

Field 5 is oneConnects
Those are the checkbox field names that need to be totaled
Big thanks!!!

C
cgphp 7/29/2011

Sorry, I don't need it. This solution should work:

var ctrlTotal = Runner.getControl(pageid,'oneScore');

var total_temp;

ctrlTotal.setValue(3);
$("input[type='checkbox']").click(function(e){

total_temp = ctrlTotal.getValue();

if($(this).is(':checked'))

ctrlTotal.setValue(parseInt($(this).val()) + parseInt(total_temp));

else

ctrlTotal.setValue(parseInt(parseInt(total_temp) - parseInt($(this).val())));

});
501385 7/29/2011



Sorry, I don't need it. This solution should work:

var ctrlTotal = Runner.getControl(pageid,'oneScore');

var total_temp;

ctrlTotal.setValue(3);
$("input[type='checkbox']").click(function(e){

total_temp = ctrlTotal.getValue();

if($(this).is(':checked'))

ctrlTotal.setValue(parseInt($(this).val()) + parseInt(total_temp));

else

ctrlTotal.setValue(parseInt(parseInt(total_temp) - parseInt($(this).val())));

});



Thank you so much. Working great on the Add page! Do I need to change anything to get it working on the edit page?

C
cgphp 7/29/2011

In the edit page:

var ctrlTotal = Runner.getControl(pageid,'oneScore');

var total_temp = 0;
$("input[type='checkbox']").each(function(e){

if($(this).is(':checked'))

{

total_temp += parseInt($(this).val());

}

});
ctrlTotal.setValue(total_temp);
$("input[type='checkbox']").click(function(e){

total_temp = ctrlTotal.getValue();

if($(this).is(':checked'))

ctrlTotal.setValue(parseInt($(this).val()) + parseInt(total_temp));

else

ctrlTotal.setValue(parseInt(parseInt(total_temp) - parseInt($(this).val())));

});


Not tested!

501386 7/29/2011



In the edit page:

var ctrlTotal = Runner.getControl(pageid,'oneScore');

var total_temp = 0;
$("input[type='checkbox']").each(function(e){

if($(this).is(':checked'))

{

total_temp += parseInt($(this).val());

}

});
ctrlTotal.setValue(total_temp);
$("input[type='checkbox']").click(function(e){

total_temp = ctrlTotal.getValue();

if($(this).is(':checked'))

ctrlTotal.setValue(parseInt($(this).val()) + parseInt(total_temp));

else

ctrlTotal.setValue(parseInt(parseInt(total_temp) - parseInt($(this).val())));

});


Not tested!


Thanks for the quick response..I tested the code. My total in the total box when I clicked edit was 13. When I opened up the page it the total in the total field was 147. The checkboxes are working on the edit page.
So now the issue is the Total amount. Man I seriously owe you something!!!

C
cgphp 7/29/2011



My total in the total box when I clicked edit was 13. When I opened up the page it the total in the total field was 147


Sorry, I don't understand what you mean. Please, could you be more clear about "it the total in the total field was 147" ?

501387 7/29/2011



Sorry, I don't understand what you mean. Please, could you be more clear about "it the total in the total field was 147" ?


Again Sorry,
when I go to the edit page...instead of the total of 13 which is what the total came to from the add page...on the edit page it is showing up as 147. It should be "13".
Again sorry for the lack of communication.
I really do owe you big time!!