Menu
Hello, I have a form that has item 1 – item 1’s cost is determined by the length and width (square feet) and by the material price. This generates totalcost (for item 1)
Same thing happens for item 2.. This generates totalcost2 (for item2)
Now I need to create a function that will grab the content/values of these totals and display a TOTAL VALUE.. I would think this would be simple but I can’t seem to figure it out.
Again totalcost + totalcost2 = grandTotal
Any help would be appreciated – function code and html for webpage.
Thx
[CODE]
var grandTotal = Number(totalcost) + Number(totalcost2);
var grandTotal = (totalcost * 1) + (totalcost2 * 1);
[/CODE]
[CODE]
function _GetTotal($a, $b) {
return ($a * 1) + ($b * 1);
}
var totalcost = 123;
var totalcost2 = 456;
var grandTotal = _GetTotal(totalcost, totalcost2);
[/CODE]
"Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"[/QUOTE]
length = parseFloat(frm.length.value);
width = parseFloat(frm.width.value);
if (length > 0 && width > 0) {
order_total = length * width;
for (i = 0; i < frm.tooling.length; i++) {
if (frm.tooling[i].value === "withouttooling" && frm.tooling[i].checked === true) {
order_total = order_total * ".30";
}
if (frm.tooling[i].value === "withtooling" && frm.tooling[i].checked === true) {
order_total = order_total * ".40";
}
}
//order_total = order_total * .35
order_total = order_total + 25;
}
else {
order_total = 0;
}
// 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
frm.totalcost.value = '$ ' + round_decimals(order_total, 2);
// 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;
length = parseFloat(frm.length2.value);
width = parseFloat(frm.width2.value);
if (length > 0 && width > 0) {
order_total2 = length * width;
for (i = 0; i < frm.tooling2.length; i++) {
if (frm.tooling2[i].value === "withouttooling" && frm.tooling2[i].checked === true) {
order_total2 = order_total2 * ".30";
}
if (frm.tooling2[i].value === "withtooling" && frm.tooling2[i].checked === true) {
order_total2 = order_total2 * ".40";
}
}
//order_total2 = order_total2 * .35
order_total2 = order_total2 + 25;
}
else {
order_total2 = 0;
}
// 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_total2 += item_quantity * item_price
//}
//}
//}
// Display the total rounded to two decimal places
frm.totalcost2.value = '$ ' + round_decimals(order_total2, 2);
// 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;
[code=html]
Grand Total: <span id="grandTotal"></span>
[/code]
[CODE]
if(typeof(_N) != "function") function _N($n, $d) { return ($d) ? Number($n).toFixed($d).toString().replace(/B(?=(d{3})+(?!d))/g, ",") : $n.toString().replace(/B(?=(d{3})+(?!d))/g, ","); }
function Calculate(frm) {
var order_total, length, width, i;
order_total = 0;
length = parseFloat(frm.length.value);
width = parseFloat(frm.width.value);
if(length > 0 && width > 0) {
order_total = length * width;
for(i = 0; i < frm.tooling.length; i++) {
if (frm.tooling[i].value === "withouttooling" && frm.tooling[i].checked === true) order_total = order_total * ".30";
if (frm.tooling[i].value === "withtooling" && frm.tooling[i].checked === true) order_total = order_total * ".40";
}
//order_total = order_total * .35
order_total = order_total + 25;
} else {
order_total = 0;
}
// Display the total rounded to two decimal places
//frm.totalcost.value = '$ ' + round_decimals(order_total, 2);
frm.totalcost.value = '$' + _N(order_total, 2);
_CalculateTotal(frm);
}
function Calculateitem2(frm) {
var order_total, length, width, i;
order_total = 0;
length = parseFloat(frm.length2.value);
width = parseFloat(frm.width2.value);
if(length > 0 && width > 0) {
order_total = length * width;
for(i = 0; i < frm.tooling2.length; i++) {
if(frm.tooling2[i].value === "withouttooling" && frm.tooling2[i].checked === true) order_total = order_total * ".30";
if(frm.tooling2[i].value === "withtooling" && frm.tooling2[i].checked === true) order_total = order_total * ".40";
}
//order_total2 = order_total2 * .35
order_total = order_total + 25;
} else {
order_total = 0;
}
// Display the total rounded to two decimal places
//frm.totalcost2.value = '$ ' + round_decimals(order_total, 2);
frm.totalcost2.value = '$' + _N(order_total, 2);
_CalculateTotal(frm);
}
function _CalculateTotal($f) {
var $total1 = parseFloat($f.totalcost.value.replace(/[^0-9.]/g, ""));
var $total2 = parseFloat($f.totalcost2.value.replace(/[^0-9.]/g, ""));
console.log($total1, $total2);
document.getElementById("grandTotal").innerHTML = (!isNaN($total1) && !isNaN($total2)) ? "$" + _N($total1 + $total2, 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;
}
[/CODE]
[code=html]
<span id="grandTotal">Grand Total: </span>
[/code]
[CODE]
// This goes inside of the _CalculateTotal() function
// Replacing the last line of the function
document.getElementById("grandTotal").innerHTML = (!isNaN($total1) && !isNaN($total2)) ? "Grand Total: $" + _N($total1 + $total2, 2) : "Grand Total: ";
[/CODE]
[CODE]
if(typeof(_N) != "function") function _N($n, $d) { return ($d) ? Number($n).toFixed($d).toString().replace(/B(?=(d{3})+(?!d))/g, ",") : $n.toString().replace(/B(?=(d{3})+(?!d))/g, ","); }
$itemCount = 2;
$lFields = ["length", "length2", "length3", "length4"];
$wFields = ["width", "width2", "width3", "width4"];
$tFields = ["totalcost", "totalcost2", "totalcost3", "totalcost4"];
function Calculate($a) {
var order_total, length, width, i;
order_total = 0;
length = parseFloat(document.forms[1][$lFiends[$a]].value);
width = parseFloat(document.forms[1][$wFiends[$a]].width.value);
if(length > 0 && width > 0) {
order_total = length * width;
for(i = 0; i < document.forms[1]["tooling"].length; i++) {
if (document.forms[1]["tooling"][i].value === "withouttooling" && document.forms[1]["tooling"][i].checked === true) order_total = order_total * ".30";
if (document.forms[1]["tooling"][i].value === "withtooling" && document.forms[1]["tooling"][i].checked === true) order_total = order_total * ".40";
}
//order_total = order_total * .35
order_total = order_total + 25;
} else {
order_total = 0;
}
// Display the total rounded to two decimal places
//frm.totalcost.value = '$ ' + round_decimals(order_total, 2);
document.forms[1]["totalcost"].value = '$' + _N(order_total, 2);
_CalculateTotal();
}
function _CalculateTotal() {
var $grandTotal = 0;
var $f = document.forms[1];
for(var $i = 0; $i < $itemCount; $i++) {
$grandTotal = ($grandTotal) + parseFloat($f[$tFields[$i]].value.replace(/[^0-9.]/g, ""));
}
document.getElementById("grandTotal").innerHTML = _N($grandTotal, 2);
}
[/CODE]
[code=html]
<input class="short" onchange="Calculate(0)" name="width">
[/code]
[code=html]
<input class="short" onchange="Calculate(1)" name="width">
[/code]
[CODE]
document.forms["myFormName"]["length"].value;
[/CODE]
[CODE]
if(typeof(_N) != "function") function _N($n, $d) { return ($d) ? Number($n).toFixed($d).toString().replace(/B(?=(d{3})+(?!d))/g, ",") : $n.toString().replace(/B(?=(d{3})+(?!d))/g, ","); }
$itemCount = 2;
$lFields = ["length", "length2", "length3", "length4"];
$wFields = ["width", "width2", "width3", "width4"];
$tFields = ["totalcost", "totalcost2", "totalcost3", "totalcost4"];
function Calculate($a) {
var order_total, length, width, i;
order_total = 0;
length = parseFloat(document.forms[1][$lFields[$a]].value);
width = parseFloat(document.forms[1][$wFields[$a]].width.value);
if(length > 0 && width > 0) {
order_total = length * width;
for(i = 0; i < document.forms[1]["tooling"].length; i++) {
if (document.forms[1]["tooling"][i].value === "withouttooling" && document.forms[1]["tooling"][i].checked === true) order_total = order_total * ".30";
if (document.forms[1]["tooling"][i].value === "withtooling" && document.forms[1]["tooling"][i].checked === true) order_total = order_total * ".40";
}
//order_total = order_total * .35
order_total = order_total + 25;
} else {
order_total = 0;
}
// Display the total rounded to two decimal places
//frm.totalcost.value = '$ ' + round_decimals(order_total, 2);
document.forms[1]["totalcost"].value = '$' + _N(order_total, 2);
_CalculateTotal();
}
function _CalculateTotal() {
var $grandTotal = 0;
var $f = document.forms[1];
for(var $i = 0; $i < $itemCount; $i++) {
$grandTotal = ($grandTotal) + parseFloat($f[$tFields[$i]].value.replace(/[^0-9.]/g, ""));
}
document.getElementById("grandTotal").innerHTML = _N($grandTotal, 2);
}
[/CODE]
length = parseFloat(frm.length.value);
width = parseFloat(frm.width.value);
if(length > 0 && width > 0) {
order_total = length * width;
for(i = 0; i < frm.tooling.length; i++) {
if (frm.tooling[i].value === "withouttooling" && frm.tooling[i].checked === true) order_total = order_total * ".35";
if (frm.tooling[i].value === "withtooling" && frm.tooling[i].checked === true) order_total = order_total * ".45";
}
//order_total = order_total * .35
order_total = order_total
} else {
order_total = 0;
}
// Display the total rounded to two decimal places
//frm.totalcost.value = '$ ' + round_decimals(order_total, 2);
frm.totalcost.value = '$' + _N(order_total, 2);
_CalculateTotal(frm);
length = parseFloat(frm.length2.value);
width = parseFloat(frm.width2.value);
if(length > 0 && width > 0) {
order_total = length * width;
for(i = 0; i < frm.tooling2.length; i++) {
if(frm.tooling2[i].value === "withouttooling" && frm.tooling2[i].checked === true) order_total = order_total * ".35";
if(frm.tooling2[i].value === "withtooling" && frm.tooling2[i].checked === true) order_total = order_total * ".45";
}
//order_total2 = order_total2 * .35
order_total = order_total;
} else {
order_total = 0;
}
// Display the total rounded to two decimal places
//frm.totalcost2.value = '$ ' + round_decimals(order_total, 2);
frm.totalcost2.value = '$' + _N(order_total, 2);
_CalculateTotal(frm);
// 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;
length = parseFloat(document.forms[1][$lFields[$a]].value);
width = parseFloat(document.forms[1][$wFields[$a]].width.value);
if(length > 0 && width > 0) {
order_total = length * width;
for(i = 0; i < document.forms[1]["tooling"].length; i++) {
if (document.forms[1]["tooling"][i].value === "withouttooling" && document.forms[1]["tooling"][i].checked === true) order_total = order_total * ".30";
if (document.forms[1]["tooling"][i].value === "withtooling" && document.forms[1]["tooling"][i].checked === true) order_total = order_total * ".40";
}
//order_total = order_total * .35
order_total = order_total + 25;
} else {
order_total = 0;
}
// Display the total rounded to two decimal places
//frm.totalcost.value = '$ ' + round_decimals(order_total, 2);
document.forms[1]["totalcost"].value = '$' + _N(order_total, 2);
_CalculateTotal();
[CODE]
function Calculate($a) {
var order_total, length, width, i;
order_total = 0;
length = parseFloat(document.forms[1][$lFields[$a]].value);
width = parseFloat(document.forms[1][$wFields[$a]].value);
if(length > 0 && width > 0) {
order_total = length * width;
for(i = 0; i < document.forms[1]["tooling"].length; i++) {
if (document.forms[1]["tooling"][i].value === "withouttooling" && document.forms[1]["tooling"][i].checked === true) order_total = order_total * ".30";
if (document.forms[1]["tooling"][i].value === "withtooling" && document.forms[1]["tooling"][i].checked === true) order_total = order_total * ".40";
}
//order_total = order_total * .35
order_total = order_total + 25;
} else {
order_total = 0;
}
// Display the total rounded to two decimal places
//frm.totalcost.value = '$ ' + round_decimals(order_total, 2);
document.forms[1][$tFields[$a]].value = '$' + _N(order_total, 2);
_CalculateTotal();
}
[/CODE]
[CODE]
function _CalculateTotal() {
var $grandTotal = 0;
var $f = document.forms[1];
for(var $i = 0; $i < $itemCount; $i++) {
var $tmpTotal = parseFloat($f[$tFields[$i]].value.replace(/[^0-9.]/g, ""));
$grandTotal = (isNaN($tmpTotal)) ? ($grandTotal) + 0 : ($grandTotal) + $tmpTotal;
}
document.getElementById("grandTotal").innerHTML = _N($grandTotal, 2);
}
[/CODE]
[code=php]
$totalcost = (isset($_POST["totalcost"])) ? (int)preg_replace("/[^0-9.]/", "", $_POST["totalcost"]) : 0;
$totalcost2 = (isset($_POST["totalcost2"])) ? (int)preg_replace("/[^0-9.]/", "", $_POST["totalcost2"]) : 0;
$grandTotal = $totalcost + $totalcost2;
[/code]
[code=html]
Number of Items:
<select id="itemCount" name="itemCount" onchange="_ChangeItemCount(this)">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<br />
<br />
<p><input class="checkbox" value="withouttooling" checked="checked" name="tooling" type="radio"> No Tooling Design<br/><br />
<input class="checkbox" value="withtooling" name="tooling" type="radio"> With tooling design<br/><br />
<small style="FONT: 10px Arial,Helvetica,sans-serif">(All tooling Gold unless otherwise specified)</small></p>
<p>Tooling design inset<br/><br />
Choose<br />
<input class="checkbox" value="1/2 inch" name="inset" type="radio"> 1/2”<br />
<input class="checkbox" value="3/4 inch" name="inset" type="radio"> 3/4”<br />
<input class="checkbox" value="1 inch" name="inset" type="radio"> 1”</p>
<p>Plus $25.00 shipping</p>
<hr/>
<br />
<div id="itemDiv1">
<strong>Item 1</strong>
<br />
Width<br />
<input class="short" onchange="Calculate(1)" name="width1"><small>(in)</small>
<br/><br />
Length<br />
<input class="short" onchange="Calculate(1)" name="length1"><small>(in)</small>
<p style="PADDING-BOTTOM: 0pt"><strong>Total Cost</strong><input class="short" onfocus="this.form.elements[0].focus()" name="totalcost1" id="totalcost1" /></p>
</div>
<div id="itemDiv2">
<strong>Item 2</strong>
<br />
Width<br />
<input class="short" type="text" name="width2" onchange="Calculate(2)" /><small>(in)</small>
<br /><br />
Length<br />
<input class="short" type="text" name="length2" onchange="Calculate(2)" /><small>(in)</small>
<p style="PADDING-BOTTOM: 0pt"><strong>Total Cost</strong><input class="short" onfocus="this.form.elements[0].focus()" name="totalcost2" id="totalcost2" /></p>
</div>
<div id="itemDiv3">
<strong>Item 3</strong>
<br />
Width<br />
<input class="short" type="text" name="width3" onchange="Calculate(3)" /><small>(in)</small>
<br /><br />
Length<br />
<input class="short" type="text" name="length3" onchange="Calculate(3)" /><small>(in)</small>
<p style="PADDING-BOTTOM: 0pt"><strong>Total Cost</strong><input class="short" onfocus="this.form.elements[0].focus()" name="totalcost3" id="totalcost3" /></p>
</div>
<br />
<br />
Grand Total: <span id="grandTotal"></span>
<script>
if(typeof(_N) != "function") function _N($n, $d) { return ($d) ? Number($n).toFixed($d).toString().replace(/B(?=(d{3})+(?!d))/g, ",") : $n.toString().replace(/B(?=(d{3})+(?!d))/g, ","); }
$itemCount = 1;
function _ChangeItemCount($a) {
$itemCount = $a.value;
document.getElementById("itemDiv1").style.display = "none";
document.getElementById("itemDiv2").style.display = "none";
document.getElementById("itemDiv3").style.display = "none";
for(var $i = 0; $i < $itemCount; $i++) {
document.getElementById("itemDiv"+($i+1)).style.display = "block";
}
_CalculateTotal();
}
_ChangeItemCount(document.getElementById("itemCount"));
function Calculate($a) {
var order_total, length, width, i;
order_total = 0;
length = parseFloat(document.forms[1]["length"+$a].value);
width = parseFloat(document.forms[1]["width"+$a].value);
if(length > 0 && width > 0) {
order_total = length * width;
for(i = 0; i < document.forms[1]["tooling"].length; i++) {
if (document.forms[1]["tooling"][i].value === "withouttooling" && document.forms[1]["tooling"][i].checked === true) order_total = order_total * ".30";
if (document.forms[1]["tooling"][i].value === "withtooling" && document.forms[1]["tooling"][i].checked === true) order_total = order_total * ".40";
}
order_total = order_total + 25;
} else {
order_total = 0;
}
document.forms[1]["totalcost"+$a].value = '$' + _N(order_total, 2);
_CalculateTotal();
}
function _CalculateTotal() {
var $grandTotal = 0;
var $f = document.forms[1];
for(var $i = 0; $i < $itemCount; $i++) {
var $tmpTotal = parseFloat($f["totalcost"+($i+1)].value.replace(/[^0-9.]/g, ""));
$grandTotal = (isNaN($tmpTotal)) ? ($grandTotal) + 0 : ($grandTotal) + $tmpTotal;
}
document.getElementById("grandTotal").innerHTML = _N($grandTotal, 2);
}
</script>
[/code]
length = parseFloat(document.forms[0]["length"+$a].value);
width = parseFloat(document.forms[0]["width"+$a].value);
if(length > 0 && width > 0) {
order_total = length * width;
for(i = 0; i < document.forms[0]["tooling"].length; i++) {
if (document.forms[0]["tooling"][i].value === "withouttooling" && document.forms[0]["tooling"][i].checked === true) order_total = order_total * ".30";
if (document.forms[0]["tooling"][i].value === "withtooling" && document.forms[0]["tooling"][i].checked === true) order_total = order_total * ".40";
}
order_total = order_total + 25;
} else {
order_total = 0;
}
document.forms[0]["totalcost"+$a].value = '$' + _N(order_total, 2);
_CalculateTotal();
[CODE]
_ChangeItemCount(document.getElementById("itemCount"));
[/CODE]
[CODE]
window.onload = function() {_ChangeItemCount(document.getElementById("itemCount")); }
[/CODE]
Thanks that did it.. I notice that when I change the selection from tooling to notooling the total cost doesn't change.
Can you take a quick look at this pleasewww.webcrawldesigns.com/dct/order-form
thanks[/QUOTE]
0.1.9 — BETA 6.2