View Full Version : Simple maths query
EmmaGale
05-16-2005, 08:44 AM
Hi can someone please help me with this query?
I am using Dreamweaver. I have an ASP/VBScript page adding information from a form to a database.
I have two fields which both a drop down box containing 5 different selections numbered 1 to 5. The third field contains a score which is the first field multiplied by the second field.
Can someone help me with the code to achieve this please.
kind regards
emma gale
brian.mark
05-18-2005, 12:14 AM
Not to complicate it more, but we're going to add another language. JavaScript to be more precise.
<Script Language="JavaScript">
<!--
function dothemath()
{
calcform.answer.value=calcform.number1.value * calcform.number2.value;
}
//-->
</Script>
<form name="calcform">
Number 1: <select name="number1" onChange="dothemath();">
<option value="1">1
<option value="2">2
<option value="3">3
<option value="4">4
<option value="5">5
</select>
Number 2: <select name="number2" onChange="dothemath();">
<option value="1">1
<option value="2">2
<option value="3">3
<option value="4">4
<option value="5">5
</select>
Score: <input type=text name="answer" value="1" size=3>
</form>
You could also make the score a hidden field to just pass it when you do your form post. It's not pretty code... that part is up to you.
Brian.
EmmaGale
05-19-2005, 08:34 AM
Thank you so so much! It works a treat!
I have got it writing to the input box, hidden the field and writing it to the page!
Wonderful stuff!
Thanks again
emma
EmmaGale
05-19-2005, 08:50 AM
Thats is great it works a treat.
I only have one small query with it.
I can make the field hidden and write the original variable to the form but this variable doesnt change at all when the calculation has taken place. Im not too good with javascript but managed to get this far:
<td width="12%" valign="top" class="entertext">
<input type="textfield" name="answer" value="0" size=3>
<script language="javascript" type="text/javascript">
document.write(form1.answer.value);
</script>
</td>
All it does is put the original value of 0.
With kind regards
emma
brian.mark
05-19-2005, 01:15 PM
I think you needed to use hidden instead of textfield.
<Script Language="JavaScript">
<!--
function dothemath()
{
calcform.answer.value=calcform.number1.value * calcform.number2.value;
}
//-->
</Script>
<form name="calcform" action="test.html" method=get>
Number 1: <select name="number1" onChange="dothemath();">
<option value="1">1
<option value="2">2
<option value="3">3
<option value="4">4
<option value="5">5
</select>
Number 2: <select name="number2" onChange="dothemath();">
<option value="1">1
<option value="2">2
<option value="3">3
<option value="4">4
<option value="5">5
</select>
<input type=hidden name="answer" value="1" size=3>
<input type=submit>
</form>
I loaded this on our server and tested it. http://www.toolbarn.com/test.html. When you submit, it'll show the answer in the URL string.
Brian.
EmmaGale
07-04-2005, 11:27 AM
Thank you for your help - that sorted that problem out nicely.
kind regards
Emma