Hi,
I am in charge of setting up an assessment form for a website. This form has 2 sections where there are checkboxes that hold numeric values. These values must be added up at the end of each section.
(ie: Section 1: 10 questions - total of the 10 questions shown at the bottom of the section
Section 2: 12 questions - total of section 2 only shown at the bottom of the that section)
Then all form choices and totals to be submitted.
I have a PHP form handler submitting the information and currently I have a .js file processing the calculation in section 1 - but I have no idea how to differentiate the script to process the second calculation of section 2 without including section 1 in the calculations.
Here is the .js I am using as a separate file:
// Calculate the total for items in the form which are selected.
function calculateTotal(inputItem) {
with (inputItem.form) {
// Process each of the different input types in the form.
if (inputItem.type == "radio") { // Process radio buttons.
// Subtract the previously selected radio button value from the total.
calculatedTotal.value = eval(calculatedTotal.value
) - eval(previouslySelectedRad
ioButton.v
alue);
// Save the current radio selection value.
previouslySelectedRadioBut
ton.value = eval(inputItem.value);
// Add the current radio button selection value to the total.
calculatedTotal.value = eval(calculatedTotal.value
) + eval(inputItem.value);
} else { // Process check boxes.
if (inputItem.checked == false) { // Item was uncheck. Subtract item value from total.
calculatedTotal.value = eval(calculatedTotal.value
) - eval(inputItem.value);
} else { // Item was checked. Add the item value to the total.
calculatedTotal.value = eval(calculatedTotal.value
) + eval(inputItem.value);
}
}
// Total value should never be less than 0.
if (calculatedTotal.value < 0) {
InitForm();
}
// Return total value.
return(calculatedTotal.val
ue);
}
}
// This function initialzes all the form elements to default values.
function InitForm() {
// Reset values on form.
document.selectionForm.tot
al.value='
0';
document.selectionForm.cal
culatedTot
al.value=0
;
document.selectionForm.pre
viouslySel
ectedRadio
Button.val
ue=0;
// Set all checkboxes and radio buttons on form to unchecked.
for (i=0; i < document.selectionForm.ele
ments.leng
th; i++) {
if (document.selectionForm.el
ements[i].
type == 'checkbox' | document.selectionForm.ele
ments[i].t
ype == 'radio') {
document.selectionForm.ele
ments[i].c
hecked = false;
}
}
}
This script works, but produces an error-not sure why (I'm no genius where JS is concerned), but it calculates it and the total is submitted in the email which is great. Now I just need to figure how to do a separate one for section 2.
The HTML page is here - if you need the code:
http://www.leveragetosucceed.com/get-help-now-assessment-2-draft.htmlThank you for any help you can give me on this issue.
Start Free Trial