Click to See Complete Forum and Search --> : help with 'multiply this by that', please


tconey
05-22-2003, 09:15 AM
I know this is basic, but I'm stumbling and would be thankful for help. Situation is: three variables;one, two, or three as choices - and I want the chosen variable's value to multiply a value in a text area to come up with a total.

I want to use radio buttons for the three choices, all have the same name :'issues', so I need the value of the chosen 'issues' to multiply the value input by viewer in 'cost' and display total.

Is this so simple that your laughter at my lack of basics knowledge prevents you from being able to type, or can you help? Great appreciation would be heaped upon you...

Jona
05-22-2003, 09:25 AM
<html><head><title></title>
<script type="text/javascript">
<!--
function getPrice(f, choice){
var finalChoice = choice.options[choice.options.selectedIndex].value;
if(!finalChoice){return true;}
f.area.value = parseInt(f.issues.value)*finalChoice
}
//-->
</script></head><body>
<form action="" name="f"><div>
<input type="radio" name="issues" value="10" onClick="getPrice(f, this.form.choices);">Ten<br>
<input type="radio" name="issues" value="20" onClick="getPrice(f, this.form.choices);">Twenty<br>
<input type="radio" name="issues" value="30" onClick="getPrice(f, this.form.choices);">Thirty<br>
<select name="choices" onChange="getPrice(this.form, this);">
<option selected value="-1">Choose one:</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select><br>
<textarea rows="5" cols="5" name="area"></textarea>
</div></form></body></html>

tconey
05-22-2003, 09:47 AM
Thanks, Jona. It's not quite it, and I blame myself for not explaining well. Perhaps looking at it is better.

On page http://www.southern-oregon-directory.com/money-saver/forms/ms-paid.htm , about 1/2 way down is 'issues' and then below that a couple of boxes is "Total cost of this ad" ( I realized as my head hit the pillow last night that it *wasn't* the total...), so I want to take the value of 'issues' times 'Total cost' and come up with the *real* total.

Is this helpful?

Jona
05-22-2003, 09:53 AM
<html><head><title></title>
<script type="text/javascript">
<!--
function getPrice(f, choice){
var finalChoice = choice.value.split("$")[1];
if(!finalChoice){return true;}
f.area.value = parseInt(f.issues.value)*parseInt(finalChoice);
}
//-->
</script></head><body>
<form action="" name="f"><div>
<input type="radio" name="issues" value="1" onClick="getPrice(this.form, this.form.amt);">One<br>
<input type="radio" name="issues" value="2" onClick="getPrice(this.form, this.form.amt);">Two<br>
<input type="radio" name="issues" value="3" onClick="getPrice(this.form, this.form.amt);">Three<br>
<input type="text" name="amt" value="$" onChange="getPrice(this.form, this);">
<textarea rows="5" cols="5" name="area"></textarea>
</div></form></body></html>

tconey
05-22-2003, 10:32 AM
Nope, still not right, ( I got a 'NaN') so I made a change to the structure. Still at http://www.southern-oregon-directory.com/money-saver/forms/ms-paid.htm , but in a more logical progression:

$ (textarea) Cost of this ad times number of issues (dropdown) =
Total Billed $ (textarea for total)

Jona
05-22-2003, 10:38 AM
Take off the [1] part of the line that declares the finalChoice variable.

tconey
05-22-2003, 12:24 PM
Okay, if at first I don't succeed, I try something else. The following code *seems* to be what I'm trying to accomplish, but I get 'error on page' and it doesn't calculate. And as long as I'm asking, if I put a form within a form, will it mess up the output data? Like, not show up in the report?

<html><head><title></title>
<script type="text/javascript">
function getTotal(f) {
f.total = f.cost.value * f.issues[f.issues.selectedIndex].value;
}
</script>
</head><body>
<table width="394" border="0" cellspacing="0" cellpadding="0">
<tr>
<form>
<td colspan="2"><b> $</b>
<input maxlength=7 size=10 name="cost">
<font color="#ff0000"> </font> <b>Cost of this ad </b>times <b>number
of issues</b>
<select name="issues">
<option selected>Choose</option>
<option value="1">One issue</option>
<option value="2">Two issues</option>
<option value="3">Three issues</option>
</select>
<br>
<b>Total Billed</b> <b>$</b>
<input type="text" name="total" size="10">
<input type="button" value="Calculate" onclick="getTotal(this.form);" />
</td>
</form>
</tr>
</table>
</body></html>

Jona
05-22-2003, 12:38 PM
//try:
f.total.value =

tconey
05-22-2003, 12:51 PM
Beautiful! It worked!

...except...

It loses decimal places in the translation. 13.40 turns into 1340 and the total is 2680 instead of 26.80.

Thanks SO MUCH for your help!

Tim

Jona
05-22-2003, 12:59 PM
//change parseInt() to parseFloat

tconey
05-22-2003, 02:44 PM
Thanks for the help! It works fine now, and I can't tell you how much I appreciate it!

Tim

Jona
05-23-2003, 09:29 AM
You're welcome. ;)