So I’m working on an order form, where people can select different quantities of items, and it then uses JS to calculate to the totals. So for example you can select 2 Brown hats ($15 each) and the TOTAL automatically becomes $30. In the POST page I can then pull the name of the text box and know they ordered 2 brown hats.
Here is my problem: I want to set up a radio button option that will allow them to select one item only, and have it add to that final total here is what it looks like:
<input type="radio" value="0" name="Syllabus" checked="No" /> Paper ($0)<br />
<input type="radio" value="0" name="Syllabus" checked="No" /> USB ($0)<br />
<input type="radio" value="20" name="Syllabus" checked="No" /> Both ($20)<br />
So how can I use JS to add $20 to the total if “BOTH” is selected? And then of course $20 is removed if they select one of the other options. And finally, if they do select PAPER or USB, how do I know in the final output what they selected, since the value will be 0 either way?
Thanks for the help!
—— IF IT HELPS ANYONE HERE IS THE CODE I USE TO ADD THE VALUES OF THE OTHER TEXT BOXES, I THEN PAD THE TOTAL WITH ZEROS AND ROUND IT ——
// JavaScript Document
function CalculateTotal(frm) {
var order_total = 0
var syllabuscost;
var radios = document.getElementsByTagName('input');
var radiovalue;
for (var z = 0; z < radios.length; z++) {
if (radios[z].type === 'radio' && radios[z].checked) {
// get value, set checked flag or do whatever you need to
radiovalue = radios[z].value;
//Change the syllabus cost depending on the value of the radio button
if (radiovalue == "Paper" || radiovalue == "USB") {
syllabuscost = 0
}
else if (radiovalue == "Both") {
syllabuscost = 20
}
}
}
// Run through all the form fields
for (var i=0; i < frm.elements.length; ++i) {
// Get the current field
form_field = frm.elements[i]
// Get the field's name
form_name = form_field.name
// Is it a "product" field?
if (form_name.substring(0,4) == "PROD") {
// If so, extract the price from the name
item_price = parseFloat(form_name.substring(form_name.lastIndexOf("_") + 1))
// Get the quantity
item_quantity = parseInt(form_field.value)
// Update the order total
if (item_quantity >= 0) {
order_total += (item_quantity * item_price)
}
}
}
// Display the total rounded to two decimal places Also added the syllabuscost to the total price!
frm.TOTAL.value = round_decimals(order_total+syllabuscost, 2)
}
}
function round_decimals(original_number, decimals) {
var result1 = original_number * Math.pow(10, decimals)
var result2 = Math.round(result1)
var result3 = result2 / Math.pow(10, decimals)
return pad_with_zeros(result3, decimals)
}
function pad_with_zeros(rounded_value, decimal_places) {
// Convert the number to a string
var value_string = rounded_value.toString()
// Locate the decimal point
var decimal_location = value_string.indexOf(".")
// Is there a decimal point?
if (decimal_location == -1) {
// If no, then all decimal places will be padded with 0s
decimal_part_length = 0
// If decimal_places is greater than zero, tack on a decimal point
value_string += decimal_places > 0 ? "." : ""
}
else {
// If yes, then only the extra decimal places will be padded with 0s
decimal_part_length = value_string.length - decimal_location - 1
}
// Calculate the number of decimal places that need to be padded with 0s
var pad_total = decimal_places - decimal_part_length
if (pad_total > 0) {
// Pad the string with 0s
for (var counter = 1; counter <= pad_total; counter++)
value_string += "0"
}
return value_string
}
AND THE HTML
<form>
<tr>
<th><label for="PROD_BrownShoe_175">Brown Shoe</label></th>
<td><input type="text" name="PROD_BrownShoe_175" onchange="CalculateTotal(this.form)" /> ($175) - Each</td>
</tr>
<tr>
<th><label for="PROD_BlackShoe_255">Black Shoe</label></th>
<td><input type="text" name="PROD_BlackShoe_255" onchange="CalculateTotal(this.form)" /> ($255) - Each</td>
</tr>
<tr>
<th><label for="PROD_BlueShoe_325">Blue Shoe</label></th>
<td><input type="text" name="PROD_BlueShoe_325" onchange="CalculateTotal(this.form)" /> ($325) - Each</td>
</tr>
<tr>
<th>Total $:</th>
<td><input type="text" name="TOTAL" onFocus="this.form.elements[0].focus()" /></td>
</tr>
</form>
Ok actually I went a different route and solved the problem. I’ve set it up so it just goes through ALL the radio buttons on the page, and then added a snippet that changes the syllabus cost depending on if it finds the value “paper”, “usb”, or “both”.
This allows me to scale this whole thing up, should I need to add more radio buttons with the same type of functionality.
There might be a cleaner, more efficient way to do this, but this is the best I could come up with. The responses I got helped a little, but didn’t really help solve the problem. Hope this helps someone else.