Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-21-2011, 11:31 PM   PM User | #1
merlingoth
New to the CF scene

 
Join Date: Sep 2011
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
merlingoth is an unknown quantity at this point
Simple Javascript Calculator Help

I'm new to javascript but not to programming. Here is my code:

PHP Code:
PHP Code:
print "<form name='goldcalculator'>";
print 
"<input type='hidden' name='goldspot' value='$goldvalue'>";
print 
"<select name='gunit' onchange='updateTotal();'>";
print 
"<option value='20'>Pennyweight (DWT)</option>";
print 
"<option value='31.1'>Grams (g)</option>";
print 
"</select><br>";
print 
"10K <input type='text' name='10k' onchange='updateTotal();'><br>";
print 
"14K <input type='text' name='14k' onchange='updateTotal();'><br>";
print 
"18K <input type='text' name='18k' onchange='updateTotal();'><br>";
print 
"22K <input type='text' name='22k' onchange='updateTotal();'><br>";
print 
"24K <input type='text' name='24k' onchange='updateTotal();'><br>";
print 
"TOTAL <input type='text' name='totalprice'>";
print 
"</form>"

Code:
<script language="JavaScript">
function updateTotal()
{
var u = document.goldcalculator.gunit.value;
var spotprice = document.goldcalculator.goldspot.value / u;
var gold10 = document.goldcalculator.10k.value;
var gold14 = document.goldcalculator.14k.value;
var gold18 = document.goldcalculator.18k.value;
var gold22 = document.goldcalculator.22k.value;
var gold24 = document.goldcalculator.24k.value;
var calculatedPrice = document.goldcalculator.totalprice.value;
calculatedPrice = ((spotprice*.999*gold24)+(spotprice*.916*gold22)+(spotprice*.75*gold18)+(spotprice*.585*gold14)+(spotprice*.417*gold10));
}
</script>
I don't know where to begin to debug this. It's very simple. The $goldvalue variable is non-editable and is pulled off my MySQL database and that works. They fill in the inputs via the PHP form and then it basically adds them up and spits out the total. Can someone tell me what's wrong?
merlingoth is offline   Reply With Quote
Old 09-22-2011, 01:20 AM   PM User | #2
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 814
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
Names must not start with number,
do something like this
name="n10k"


document.goldcalculator.totalprice.value
returns a string, setting it will not
alter the value of the text box
do something like this
var calculatedPrice = document.goldcalculator.totalprice;
calculatedPrice.value =

var u = document.goldcalculator.gunit.value;

u will be a string not a number , input values
are returned as strings, you must convert them
to numbers
DaveyErwin is offline   Reply With Quote
Old 09-22-2011, 01:41 AM   PM User | #3
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 814
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
Here is some code that
works in most modern
browsers.


Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="generator" content="daveyerwin">
<title>Untitled</title>
<script type="text/javascript">
function updateTotal(){
var u = document.goldcalculator.gunit.value;
var spotprice = document.goldcalculator.goldspot.value / u;
var gold10 = document.goldcalculator.n10k.value;
var gold14 = document.goldcalculator.n14k.value;
var gold18 = document.goldcalculator.n18k.value;
var gold22 = document.goldcalculator.n22k.value;
var gold24 = document.goldcalculator.n24k.value;
var calculatedPrice = document.goldcalculator.totalprice;
calculatedPrice.value = ((spotprice*.999*gold24)+(spotprice*.916*gold22)+(spotprice*.75*gold18)+(spotprice*.585*gold14)+(spotprice*.417*gold10));
}  
</script>

<style type="text/css">
</style>
<body>
<div id="content">
<?php
$goldvalue = 2000;
print "<form name='goldcalculator'>"; 
print "<input type='hidden' name='goldspot' value='$goldvalue'>"; 
print "<select name='gunit' onchange='updateTotal();'>"; 
print "<option value='20'>Pennyweight (DWT)</option>"; 
print "<option value='31.1'>Grams (g)</option>"; 
print "</select><br>"; 
print "10K <input type='text' name='n10k' onchange='updateTotal();'><br>"; 
print "14K <input type='text' name='n14k' onchange='updateTotal();'><br>"; 
print "18K <input type='text' name='n18k' onchange='updateTotal();'><br>"; 
print "22K <input type='text' name='n22k' onchange='updateTotal();'><br>"; 
print "24K <input type='text' name='n24k' onchange='updateTotal();'><br>"; 
print "TOTAL <input type='text' name='totalprice'>"; 
print "</form>";  
?>
</div>
</body>
</html>
The values returned from the form
are strings but when a string is
used in a calculation( Except Addition !)
it is automatically converted to number.
DaveyErwin is offline   Reply With Quote
Users who have thanked DaveyErwin for this post:
merlingoth (09-22-2011)
Old 09-22-2011, 02:10 AM   PM User | #4
merlingoth
New to the CF scene

 
Join Date: Sep 2011
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
merlingoth is an unknown quantity at this point
Thanks, I'll check it right now! As soon as I posted, I realized that I started it with numbers, but figured there were more problems and you guys would catch it anyway so I didn't edit it to confuse anybody.

===EDIT===

WORKS PERFECTLY! THANKS SO MUCH!!!

Last edited by merlingoth; 09-22-2011 at 02:22 AM.. Reason: Update
merlingoth is offline   Reply With Quote
Reply

Bookmarks

Tags
calculator, html, javascript, programming, script

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:39 AM.


Advertisement
Log in to turn off these ads.