/    Sign up×
Community /Pin to ProfileBookmark

Checkbox values question

I have a form that is summing up the total items the user has selected based on form boxes. I used a tutorial online and everything is working fine but I have a curve ball. Everything I have seen sums the “value” of the checkbox. I need some help totaling a different variable inside the form. Here is an example:

[code=html]<input type=”checkbox” name=”ProductID[]” value=”Prod1″ cost=”10″/>
<input type=”checkbox” name=”ProductID[]” value=”Prod2″ cost=”5″/>
<input type=”checkbox” name=”ProductID[]” value=”Prod3″ cost=”8″/>[/code]

I need to pass the Product IDs as an array to my next page but I need to total the cost.

The current function I am using inside my form is this:

[CODE]onclick=’this.form.total.value=calculateTotal(this);’[/CODE]

Here is the associated function:

[CODE]// 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(previouslySelectedRadioButton.value);
// Save the current radio selection value.
previouslySelectedRadioButton.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(formatCurrency(calculatedTotal.value));
}
}

// Format a value as currency.
function formatCurrency(num) {
num = num.toString().replace(/$|,/g,”);
if(isNaN(num))
num = “0”;
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = “0” + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3)) + ‘,’ + num.substring(num.length-(4*i+3));
return (((sign)?”:’-‘) + ‘$’ + num + ‘.’ + cents);
}

// This function initialzes all the form elements to default values.
function InitForm() {
// Reset values on form.
document.selectionForm.total.value=’$0′;
document.selectionForm.calculatedTotal.value=0;
document.selectionForm.previouslySelectedRadioButton.value=0;

// Set all checkboxes and radio buttons on form to unchecked.
for (i=0; i < document.selectionForm.elements.length; i++) {
if (document.selectionForm.elements.type == ‘checkbox’ | document.selectionForm.elements.type == ‘radio’) {
document.selectionForm.elements.checked = false;
}
}
}[/CODE]

My experience is with php and none of my trial and error approaches have worked with this form… What do I need to do to total the cost instead of the value?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@DokOct 15.2008 — First of all you don't need the eval statements
[CODE]calculatedTotal.value = eval(calculatedTotal.value) - eval(previouslySelectedRadioButton.value);[/CODE]
should just be
[CODE]calculatedTotal.value = calculatedTotal.value - previouslySelectedRadioButton.value;[/CODE]
Second this statment (and the other statements adding to the total value)
[CODE]calculatedTotal.value = eval(calculatedTotal.value) + eval(inputItem.value);[/CODE]
should be
[CODE]calculatedTotal.value = calculatedTotal.value * 1 + inputItem.value * 1;[/CODE]
Whenever you read or write to an input field the values are of type String. When using the + operator on strings you are concatenating. To illustrate
[CODE]
var x = '1';
var y = '2';
// z becomes '12'
z = x + y;
[/CODE]

To convert between String and Number simply multiply the string value with 1.
[CODE]
var x = '1';
var y = '2';
// z becomes 3
z = x * 1 + y * 1;
[/CODE]
Copy linkTweet thisAlerts:
@mickncauthorOct 15.2008 — Thanks for helping me clean up the code.

Did you understand the original question... how can I total the cost variable instead of the value variable?

I have tried several things but it just seems to crash the whole thing.
Copy linkTweet thisAlerts:
@DokOct 16.2008 — how can I total the cost variable instead of the value variable?[/QUOTE]What do you mean by that?
Copy linkTweet thisAlerts:
@mickncauthorOct 16.2008 — In this form:
[CODE]<input type="checkbox" name="ProductID[]" value="Prod1" cost="10"/>
<input type="checkbox" name="ProductID[]" value="Prod2" cost="5"/>
<input type="checkbox" name="ProductID[]" value="Prod3" cost="8"/>[/CODE]


The javascript is totaling the value... Well I need to add another field for it to total (ex. cost). The reason is that the function of the checkbox is to post an array of the productids to the next page for insertion into a table. That means that the value must be the productid but I also need to total the cost as the user selects boxes.

Does that make more sense?
×

Success!

Help @micknc spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...