/    Sign up×
Community /Pin to ProfileBookmark

Number will not Multiply Because of JS use? Please help!!

I’ve got this problem where I’m using JS to grab today’s currency rates and I’m trying to multiply that currency rate with a value that the user enters.

I’ve created another page called currency.php just to try and troubleshoot the problem, but I can’t seem to get it.

If I echo $var1, it will give the conversion perfectly fine.

[code=php]
$var1 = “<script>currency_show_conversion(100,’USD’,’CAD’);</script>”;
[/code]

Now if I try to multiply $var1 * $var2, the result will be 0. Does anyone know why this is happening or how I can fix it?

[code=php]
$var1 = “<script>currency_show_conversion(100,’USD’,’CAD’);</script> CAD<br>”;
$var2 = 20;
$calculation = $var1 * $var2;

echo $calculation;
[/code]

EDIT:
I should also note that I’ve tried this and it does not work.

[code=php]
$var1 = “<script>currency_show_conversion(100,’USD’,’CAD’)</script>”;
$var_int = $var1;
$var2 = 20;
$var = $var_int * $var2;

echo $var;
[/code]

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@tirnaMay 07.2010 — You are trying to run javascript functions on the server which won't work.

JS is not executed until the web page is returned to the user's browser. JS is a client side scripting language. PHP is server side.

You can get PHP to generate javascript code but you must remember that js code will not be run until the web page is sent to the browser. The JS will not run on the server.
Copy linkTweet thisAlerts:
@smohauthorMay 07.2010 — Oh no!

Is there any way I can get these currency rates using JS then? Or would I have to find a site that provides it as PHP?
Copy linkTweet thisAlerts:
@smohauthorMay 08.2010 — nevermind! I found out how to use Yahoo Finance API! Works great! THanks for the tip tirna
Copy linkTweet thisAlerts:
@tirnaMay 08.2010 — 

If I echo $var1, it will give the conversion perfectly fine.
[code=php]
$var1 = "<script>currency_show_conversion(100,'USD','CAD');</script>";
[/code]

[/quote]


To be honest, I am not sure what you are trying to do. All I can see from your posted code is that you are trying to run js functions on a server.

[B]You say echo $var1 works ok, but I can't see how it can work at all assuming currency_show_conversion() is a js function.[/B]
Copy linkTweet thisAlerts:
@smohauthorMay 08.2010 — It works to convert single numbers. So for example, if I had a rate of $1 USD, it will convert that fine to $1.04 CAD.

The problem I had was when combining numbers. So for example, $1.04 CAD (converted amount) to * 200 apples. Because the 200 apples wouldn't convert.

Regardless, yahoo finance has a good API to make calls and it all works because it is in PHP. Works fine for me now that I'm using yahoo.
×

Success!

Help @smoh spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...