/    Sign up×
Community /Pin to ProfileBookmark

Help with Calculating Form

Hello all.

I currently have a form that auto does the math for when a person adds a number to a quantity text field. Currently the amount for the quantity field comes from the name of the field. The quantity field pertains to a dropdown next to it with sizes in it. The problem I am having is that all the sizes have different prices. So as it stands right now the price is hard coded for all. I need it to be able to change based on the drop downs choice.

I hope this makes sense. If not let me know how I can explain it better. Here is my code.

[code]
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>

<head>
<meta http-equiv=”Content-Language” content=”en-us” />
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Form</title>
<script language=”JavaScript” type=”text/javascript”>
<!–

function CalculateTotal(frm) {
var 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.TOTAL.value = round_decimals(order_total, 2)
frm.TOTAL2.value = round_decimals(order_total, 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
}

//–>
</script>

</head>

<table>
<tr>
<td>
<input class=”AMOUNT” name=”TOTAL” onfocus=”this.form.elements[2].focus()” value=”0.00″ />
</td>
</tr>
<tr>
<td>
<select name=”size”>
<option selected=”selected”>-</option>
<option>Sm</option>
<option>M</option>
<option>L</option>
<option>XL</option>
<option>2XL</option>
<option>3XL</option>
</select></td>

<td>
<input type=”text” name=”PROD_StormBlue-8535_14.96″ onChange=”CalculateTotal(this.form)” style=”width: 24px” />
</td>
</tr>
</table>[/code]

You can see above the price comes from the name of the text field. PROD_StormBlue-8535_14.96. The price is the end number, 14.96. Is there any way to have that number be dynamic based on the options of the dropdown instead of just a flat number? I appreciate any help on this.

Thanks again.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@FangSep 04.2008 — Like giving the options a value that is added to the basic price?
Copy linkTweet thisAlerts:
@designopauthorSep 04.2008 — That would work, or for the options value to overwrite the basic price.

Either way really. SM M L XL are all 15.00. 2XL is 16.00 and 3XL is 17.00.

Thanks for any help.
Copy linkTweet thisAlerts:
@designopauthorSep 05.2008 — Any possibilities?
Copy linkTweet thisAlerts:
@FangSep 05.2008 — Why have a price in the input field that has no relationship to the price of the article?
×

Success!

Help @designop 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 5.29,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...