We help IT Professionals succeed at work.

Interactive MySQL Query on a webpage

swedesamurai asked
Medium Priority
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

Watch Question

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 http://www.prototypejs.org/

Hope this helps

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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.