Web Hosting Geeks | Web Hosting Experts
Need help? Call us 1(866)TOP-HOST

14120 reviews 1680 hosting providers

Questions & Answers

Ajax and PHP- How to change textbox value based on dropdown list with

May 8, 2012 by A007
hi, Does anyone know how to change the value of the textbox using Ajax and PHP, based on changing the value of the dropdown list. I have a list of currencies of countries. I want the value of the text book to change every time the value pf the dropdown list changes.
Can anyone please help me do this?
Thank you

1 Answer

0 votes
May 8, 2012 by expert
hi
try this code

HTML Code
<select name="country" onChange="getCurrencyCode('find_ccode.php?country='+this.value)">
 <option value="">Select Country</option>
        <option value="1">USA</option>
        <option value="2">UK</option>
        <option value="3">China</option>
</select>
<input type="text" name="cur_code" id="cur_code" >
JavaScript Code
function getCurrencyCode(strURL)
{
  var req = getXMLHTTP();
  if (req)
  {
               req.onreadystatechange = function()
        {
          if (req.readyState == 4)
          {                if (req.status == 200)
                {
                        document.getElementById('cur_code').value=req.responseText;
                }
                else
                {
                        alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                }
          }
        }
        req.open("GET", strURL, true);
        req.send(null);
  }
}
PHP code
<?php
$country=$_REQUEST['country'];
switch($country)
{
        case "1" :
                echo "USD";
                break;
        case "2" :
                echo "GBP";
                break;
        case "3" :
                echo "YEN";
                break;
}
?>