Skip to content

Building an AJAX currency converter with PHP, jQuery and Google

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:

[code lang=”javascript”]
$(document).ready(function() {

$(‘#submit’).click(function(){

//Get the values
var amount = $(‘#amount’).val();
var from = $(‘#from’).val();
var to = $(‘#to’).val();

var params = “amount=” + amount + “&from=” + from + “&to=” + to;

$.ajax({
type: “POST”,
url: “convert.php”,
data: params,
success: function(data){
$(‘#results’).html(data);
}
});
});
});
[/code]

We are making an ajax call to convert.php and on success filling the data into results div.

And here is convert.php :

[code lang=”php”]
// sanitizing input using built in filter_input available from PHP 5.2
$amount = filter_input(INPUT_POST, ‘amount’, FILTER_VALIDATE_INT);
$from = filter_input(INPUT_POST, ‘from’, FILTER_SANITIZE_SPECIAL_CHARS);
$to = filter_input(INPUT_POST, ‘to’, FILTER_SANITIZE_SPECIAL_CHARS);

// building a parameter string for the query
$encoded_string = urlencode($amount) . urlencode($from) . ‘%3D%3F’ . urlencode($to);

$url = ‘http://www.google.com/ig/calculator?hl=en&q=’ . $encoded_string;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);

$results = curl_exec($ch);

// this is json_decode function if you are having PHP < 5.2.0 // taken from php.net $comment = false; $out = '$x='; for ($i=0; $i

Very cool. Thank you for sharing.

  • i get this error PHP Parse error: syntax error, unexpected ‘;’ in /home/forumed/public_html/counter/currency/convert.php(39) : eval()’d code on line 1

    can you tell me why, or what i am doing wrong?

  • 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?

    Thanks!

  • 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 !

  • Fatal error: Call to undefined function curl_init() in Z:homelocalhostwwwPhp_lesson1convert.php on line 14
    what is the problems???

  • 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.

    Is there a fix for this?
    Cheers!

  • Same prob with me
    I think ajax not returning proper html value for ” , ” .

    302 U.S. dollars = 15�624.1916 Indian rupees

  • In so far as Davids comment :

    “PHP Parse error: syntax error, unexpected ‘;’ in /home/forumed/public_html/counter/currency/convert.php(39) : eval()’d code on line 1”

    Find all instances of “&amp;” in the code and replace with simply “&” and all will work fine.

    Thank you Zvonko!

  • as for the other error concerning � –

    What you need to do is use

    echo utf8_encode($x[‘lhs’] . ‘ = ‘ . $x[‘rhs’]);

    in place of

    echo $x[‘lhs’] . ‘ = ‘ . $x[‘rhs’];

    on line 39

  • Comments are closed.