Forum Moderators: open
I have a piece of javascript embedded within some PHP code. I would like to have the javascript display two prices, one in US dollars and a second price which is in Euro's.
The Euro's exchange rate would be pulled from PHP/MySQL. So what i'm looking for is the javascript to calculate the second price based on the current days exchange rate.
I'm very new to javascript, this has been bugging me for some time, very appreciative of any help / pointers.
Many thanks
Marcus
<html>
<head>
<title> Small JavaScript Example </title>
<script type="text/javascript">
<!--
function FormatNumber(num)
{
if (isNaN(num))
{
num = "0";
}
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
{
cents = "0" + cents;
}
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
{
num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
}
return (((sign)?'':'-') + num + '.' + cents);
}
function showPrice()
{
var myTotalPrice = 0;
var showUP = 0;
var myMathProblem = "";
if (document.main!= null)
{
myItemPrice = parseFloat(document.main.nuPrice.value);
for (var i = 0; i < document.main.elements.length; i++)
{
var e = document.main.elements[i];
if ( e.type == 'select-one' )
{
Item = e.selectedIndex;
myPrice = e.options[Item].text;
myDollarSign = myPrice.indexOf("$",0)
if ( myDollarSign!= "-1" )
{
myParSign = myPrice.indexOf(")", myDollarSign);
myAttributeString = myPrice.substring(myDollarSign+1, myParSign);
myAttributeString = myAttributeString.replace(/,/,"");
myAttributePrice = parseFloat(myAttributeString);
myMathProblem = myPrice.charAt(myDollarSign - 1);
}
else
{
myAttributePrice = 0;
}
if (myMathProblem == "+")
{
myTotalPrice = myTotalPrice + myAttributePrice;
}
else
{
myTotalPrice = myTotalPrice - myAttributePrice;
}
}
}
}
else
{
myTotalPrice = 0;
myItemPrice = 0;
}
myTotalPrice = FormatNumber(myTotalPrice + myItemPrice);
document.getElementById("productNEWprice").innerHTML = "Total Price with Options $" + myTotalPrice;
}
//-->
</script>
</head>
<body onload='showPrice()'>
<form id="main" name="main" action="">
<input type="hidden" name="nuPrice" value="999.99">
<select name="brian[]" onChange="showPrice();">
<option> Select One </option>
<option value="1">10 (+$1315.00)</option>
<option value="2"> 2 (+$20.00) </option>
</select>
<select name="brian[]" onChange="showPrice();">
<option> Select One </option>
<option value="1"> 1 (+$15.00)</option>
<option value="2"> 2 (+$10.00) </option>
<option value="3"> 3 (-$21.00) </option>
</select>
<br><br><div id="productNEWprice"></div>
</form>
</body>
I like the idea of using php to write some javascript statements defining the values! - can someone give me a few lines of code that i could possibly start to play with?
i have the Euro currency value stored in MySQL - i basically need to do the maths on the client side (javascript) to convert US$ to Euro in order to display both the prices.
many thanks for your help.
cheers
Marcus
Thnaks for your replies - making sense now and i see how i can query for the currency and store in the VAR - brillant!
From the code i originally included, would i have an additional line something sililar to the following:
myTotalPriceEUR = FormatNumber(myTotalPrice + myItemPrice) * $eruo;
Thanks again for your help
Regards
Marcus
This is what it's looking like on the server-side
<script type="text/javascript">
<?php print("var euroRate = " . $euroMYSQL . " ;\n");?>
</script> This is what it's looking like on the client-side in the browser and also what the server is actually sending to the browser
<script type="text/javascript">
var euroRate = 1.12345 ;
</script> As I've no idea as to how you call your MySQL database from you php script and what it returns this is at present the best advise I can give.
So: you are getting the php-variable $euroMYSQL from your call to your mysql database (somehow), and you are then subsequently inserting this value in a javascript and consequently converting the value of the php-variable $euroMYSQL to a javascript variable called 'euroRate'
I got around to trying this tonight. I added the code to my PHP and i get the following when i launch a browser viewing the page.
A runtime error has occured:
error: Invalid character.
I opened the debugger and saw the following:
<script type="text/javascript">
var euroRate = £ ;
</script>
Now, this looks all good to me, except i think it's missing the "" around the £ - perhaps it should be "£"?
Your thoughts?
Thanks for your help.
cheers
Marcus