1

I have a package table. I want to change automatically the currency of each amount depending on the location of the user. For example, if the user is from UK, it will change to pounds, if from Australia it will be AUD. I have looked for a tutorial in google but all I can see is a conversion table, like this one not. How will I do this using HTML and javascript?

<style>
    td{
        text-align: center;
    }
</style>

<table>
    <tr>
        <td></td>
        <td>
            $100
        </td>
        <td>
            $200
        </td>
        <td>
            $300
        </td>
    </tr>
    <tr>
        <td>
            Package A
        </td>
        <td>
            <input type="checkbox" checked disabled/>
        </td>
        <td>
            <input type="checkbox" checked disabled/>
        </td>
        <td>
            <input type="checkbox" checked disabled/>
        </td>
    </tr>
    <tr>
        <td>
            Package B
        </td>
        <td>
            <input type="checkbox" disabled/>
        </td>
        <td>
            <input type="checkbox" checked disabled/>
        </td>
        <td>
            <input type="checkbox" checked disabled/>
        </td>
    </tr>
    <tr>
        <td>
            Package C
        </td>
        <td>
            <input type="checkbox" disabled/>
        </td>
        <td>
            <input type="checkbox" disabled/>
        </td>
        <td>
            <input type="checkbox" checked disabled/>
        </td>
    </tr>
</table>
Jennifer
  • 1,291
  • 1
  • 11
  • 16
  • buddy @Jennifer, plz provide ur javascript try code... – Vikrant Mar 09 '15 at 06:22
  • 2
    use a select box for location selection and in selection changed event take the current location from selection dropdown then with that you can set the corresponding currency – Arunprasanth K V Mar 09 '15 at 06:22
  • haven't tried to code it in javascript because I don't have an idea on how to do that, if you have some please tell. thank you @Victor – Jennifer Mar 09 '15 at 06:24
  • @Jennifer, how u r going to get the location of user? – Vikrant Mar 09 '15 at 06:25
  • Have you explored on using Resource Files? – sms Mar 09 '15 at 06:32
  • that's also part of the problem. is it possible? like if you are from Aussie, automatically the currency you will see is AUD. Or the best possible way here is, to enter the location then I will just enter some codes like if Aussie is selected then it will convert @Victor – Jennifer Mar 09 '15 at 06:36
  • the easiest thing to do is to ask the location of the user via dropdown, or if you want to get their location automatically, you can use geoIP but you wont just be using html and js to do that – Kelvin Barsana Mar 09 '15 at 06:37
  • i would like to see it also using what you said the geoIP. Can you post it in the Answer section?@Victor – Jennifer Mar 09 '15 at 07:06

2 Answers2

0

first, get the user's ip address

$ip =  $_SERVER['REMOTE_ADDR'];
echo $location = file_get_contents("http://api.hostip.info/country.php?ip=$ip");

it will echo the two letter country code of the user, if you will test it in your local host, replace the first line with this test ip

$ip = '121.1.11.166';

the test will echo 'PH' , meaning the ip of the user is from Philippines.

seen it here: link

Community
  • 1
  • 1
Kelvin Barsana
  • 824
  • 12
  • 28
0

You may want to take look at Globalize.js which can handle many different formats including currency.

https://github.com/jquery/globalize

https://github.com/jquery/globalize/blob/master/doc/api/currency/currency-formatter.md

Those formats are based on different language settings which are available here https://github.com/jquery/globalize/blob/master/doc/cldr.md.

Using this you can even handle currency symbols but also display format of your data.

To get the language of your visitor you can use something like this

Var language = window.navigator.userLanguage || window.navigator.language;
alert(language); //works IE/SAFARI/CHROME/FF

But keep in mind that one USD $ !== one Euro € ^^

Stephan Ahlf
  • 3,310
  • 5
  • 39
  • 68