Forum Moderators: open

Message Too Old, No Replies

can i call a php variable from javascript?

Want to simply calculate Euro currency from US price.

         

marcus76

4:59 pm on Aug 9, 2004 (gmt 0)

10+ Year Member



Hello

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>

BjarneDM

8:24 pm on Aug 9, 2004 (gmt 0)

10+ Year Member



you can't access php variables directly from javascript:
a) php is serverside
b) javascript is clientside

you have several options:
1) use php to write some javascript statements defining the values
2) use php to write the values to a file that's then linked to in the website

marcus76

8:37 am on Aug 10, 2004 (gmt 0)

10+ Year Member



thanks for the reply.

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

BjarneDM

11:21 am on Aug 10, 2004 (gmt 0)

10+ Year Member



that's simple!

<script type="text/javascript">

<?php print("var euro = " . $euro . " ;\n");?>

</script>

marcus76

2:21 pm on Aug 10, 2004 (gmt 0)

10+ Year Member



Hey BjarneDM,

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

BjarneDM

7:40 pm on Aug 10, 2004 (gmt 0)

10+ Year Member



No no no no no - you seem to completely misunderstanding what's happening with the php script (correct me if I'm wrong). The php script is inserting a javascript sentence into the javascript code defining the value of the euro. You mustn't prefix that variable with '$' when using it. Going back to my example (enhanced):

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'

marcus76

8:54 pm on Aug 16, 2004 (gmt 0)

10+ Year Member



Hey BjarneDM,

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

marcus76

10:58 am on Aug 17, 2004 (gmt 0)

10+ Year Member



ok, i'm told it should be:

<?php print("var euroRate = """ . $currency . """ ;\n");?>

marcus76

7:57 am on Aug 18, 2004 (gmt 0)

10+ Year Member



i didnt work ;-(

i get the following error:

Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING

Anyone know of the correct syntax?

Thanks

Marcus