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

2,484 views
asked May 8, 2012 in General by A007 (360 points)
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 like 0 dislike
answered May 8, 2012 by expert (14,380 points)
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;
}
?>
...