As you may already know, Google has a calculator feature built in their search engine, which can be used to convert measures, currencies, get stock quotes, weather data and many other features. You can see this features explained here.
Why wouldn’t we use it for something useful and build a simple AJAX currency converter using Google currency converter feature? We need some time, jQuery, Google, great music and of course, this tutorial.
So, let’s start. Create new file, call it index.php and make some basic markup for our converter:
[code lang=”html”]
[/code]
Basically, we have two drop downs with currencies, amount field and a button. Below is a div with id results which will hold our result data.
Now let’s add some jQuery to handle AJAX call and filling result data into our markup:
Hi ! I was wondering how should I change the code if I want to use an xml file provided with the currrency rate data instead of Google api? Is it something that can be easily done here?
Hi Chris, unfortunately there is no quick change in code that would help you with that.
As you can see, Google is returning JSON and you need to parse an XML file. But, check out the SimpleXML library on Google, it could help you. ( http://php.net/manual/en/book.simplexml.php )
Marco
Hi there, this is a nice script. I have found a ton of this scripts but they all use a one simple conversion with input boxes and submit buttons.
Is there any way to adapt this code so it can change several prices at once ?
Imagine you have a html table with several prices and you want to convert all the prices at once to a certain currency by clicking a country flag or selecting via select list.
Now, that would be a “killer” !
Thanks for any help !
acid
Fatal error: Call to undefined function curl_init() in Z:homelocalhostwwwPhp_lesson1convert.php on line 14
what is the problems???
Probably Curl library is not installed on the server. Contact your hoster.
noush
Goof info
Do you have any idea how to integrate this in wordpress?
Thanks
Dave
Hi there.
This is great stuff.
One bug though:
With values greater than 999, the code inserts a comma (presumably), which is turnig out as an undefined character (‘?’ in a diamond (Firefox utf-8)
Looks like this: 1�000.
Very cool. Thank you for sharing.