Interactive MySQL Query on a webpage

Posted on 2009-02-12
Last Modified: 2012-05-06
Hello Experts,
I have a web page where I want to let the user select a Currency from a dropdown menu.  
Using this value connect to a MySQL Table that lists the currency and exchange rate and using the currency as the selector return the exchange rate so I can calculate a price in the Selected Currency.
I can do a My SQL Query that will do this and can start it with PHP-That is query the table with the Currency and returning the exchange rate.
What I can't seem to figure out is how I could make this query with Javascript I do not have to write anything into this Table just retrieve the exchange rate.
I do not have ASP.NET or AJAX just a very simple site.
Thanks in advance.
Pseudo-Very Pseudo:

1.  From Drop Drown:

<select name="KDNWALO1" id="KDNWALO1" onChange="ShowCurr1();ShowCurr1a();"  style="width:180px; text-align:left; color: #0000FF; font-weight: bold; font-size: 10px;"/>    

                								<option selected="selected">Please Select Currency:</option>

                                                <option value="AUD">Australian Dollars</option>

                                                <option value="GBP">British Pounds</option>

                                                <option value="BGN">Bulgarian Leva</option>

                                                <option value="CAD">Canadian Dollars</option>

                                                <option value="CNY">Chinese Yuan Renminbi</option>

                                                <option value="HRK">Croatian Kuna</option>

                                                <option value="CYP">Cyprian Pounds</option>

                                                <option value="CZK">Czech Koruny</option>

                                                <option value="DKK">Danish Kroner</option>

                                                <option value="EEK">Estonian Krooni</option>

                                                <option value="EUR">Euros</option>

                                                <option value="HKD">Hong Kong Dollars</option>

                                                <option value="HUF">Hungarian Forint</option>

                                                <option value="ISK">Icelandic Kronur</option>

                                                <option value="IDR">Indonesian Rupiahs</option>

                                                <option value="JPY">Japanese Yen</option>

                                                <option value="LVL">Latvian Lati</option>

                                                <option value="LTL">Lithuanian Litai</option>

                                                <option value="MYR">Malaysian Ringgits</option>

                                                <option value="MTL">Malta Liri</option>

                                                <option value="NZD">New Zealand Dollars</option>

                                                <option value="NOK">Norwegian Krone</option>

                                                <option value="PHP">Philippine Pesos</option>

                                                <option value="PLN">Polish Zlotych</option>

                                                <option value="RON">Romanian New Lei</option>

                                                <option value="RUB">Russian Rubles</option>

                                                <option value="SGD">Singapore Dollars</option>

                                                <option value="SKK">Slovakian Koruny</option>

                                                <option value="SIT">Slovenian Tolars</option>

                                                <option value="ZAR">South African Rand</option>

                                                <option value="KRW">South Korean Won</option>

                                                <option value="SEK">Swedish Kronor</option>

                                                <option value="CHF">Swiss Francs</option>

                                                <option value="THB">Thai Baht</option>

                                                <option value="TRY">Turkish New Lira</option>

                                                <option value="USD">US Dollars</option>


2.  Query the MySQL File-Connects etc ommitted:

// Run the connection here 

// Run the connection here 

@mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");

@mysql_select_db("$db_name") or die ("no database");

// Query member data from the database and ready it for display

$sql = mysql_query("SELECT * FROM engcur WHERE currency='$KDNWALO1'"); 

while($row = mysql_fetch_array($sql)){ 

$currency = $row[currency];

$rate = $row[rate];


3.  What is missing is a Javascript that will enable me to execute this query and retieve the $Rate variable

Open in new window

Question by:swedesamurai
    LVL 2

    Expert Comment

    What your asking for will require AJAX (which is something handled by JavaScript).

    The process is something like:

    1. User selects 'Currency'
    2. Javascript picks up the value of 'Currency' and then (using AJAX) passes it to your database lookup (PHP page)
    3. The response is then passed back to javascript which then uses the exchange rate to calculate your price

    Making the most out of AJAX will involve a good understanding of Javascript. I can recommend the 'Prototype.js' framework which simplifies many methods

    Hope this helps

    LVL 19

    Accepted Solution

    well actually. because the rates are all known at the time the page is displayed they could all be included in the java script.

    Just as you create the option list with php code you'd create the java if statements.

    Author Closing Comment

    Took Michael701:advice and loaded the rates into hidden fields used a javascript tied into the value of the selection and used javascript to calculate rate changes based on currency change -works elegantly without ajax.  Thanks for the tip

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now