title: Populate a text box based on a dynamic drop down box
body: <p>I am trying to populate a text box based on a dynamic dropbox that is populated from the database.</p>
  
  <p>My Code is as Below : </p>
  
  <p><strong>index.php</strong></p>
  
  <pre><code>&lt;?php
      include "../conn.php";
  ?&gt;
  &lt;html&gt;
      &lt;head&gt;
      &lt;title&gt;Changing textbox value based on dropdown list using Ajax and PHP&lt;/title&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
      &lt;script&gt;
      function getXMLHTTP() { 
              var xmlhttp=false;  
              try{
                  xmlhttp=new XMLHttpRequest();
              }
              catch(e)    {       
                  try{            
                      xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
                  }
                  catch(e){
                      try{
                      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                      }
                      catch(e1){
                          xmlhttp=false;
                      }
                  }
              }
  
              return xmlhttp;
          }
  
  
  
      function getCurrencyCode(strURL){       
          var req = getXMLHTTP();     
          if (req){
              //function to be called when state is changed
              req.onreadystatechange = function(){
                  //when state is completed i.e 4
                  if (req.readyState == 4) {          
                      // only if http status is "OK"
                      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);
          }           
      }
      &lt;/script&gt;
      &lt;/head&gt;
  
      &lt;body style="font: 12px Verdana, Arial, Helvetica, sans-serif;"&gt;
  
      &lt;form style="text-align:center" method="post" action="" name="form1"&gt;
      &lt;p style="color:#000099 "&gt;When you change the dropdown list, the respective currency code of the country will be displayed in the textbox which is fetched from PHP using Ajax. &lt;/p&gt;
      &lt;p&gt;Department :                             &lt;?PHP
                                      echo "&lt;select name= 'Department' class='form-control selectpicker' onChange='getCurrencyCode('find_ccode.php?country='+this.value)' Required&gt;";
                                      echo '&lt;option value=""&gt;'.'--Please Select Department--'.'&lt;/option&gt;';
                                      $sql = "SELECT ID,Name FROM Departments";
                                      $query = sqlsrv_query($conn,$sql);
                                      $query_display = sqlsrv_query($conn,$sql);
                                      while($row=sqlsrv_fetch_array($query_display,SQLSRV_FETCH_ASSOC)){
                                      echo "&lt;option value='". $row['Name']."'&gt;".$row['Name']. '&lt;/option&gt;';
                                      }
                                      echo "&lt;/select&gt;";
                                  ?&gt;
      ID :   &lt;input type="text" name="cur_code" id="cur_code" &gt;&lt;/p&gt;
      &lt;/form&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>find_ccode.php</strong></p>
  
  <pre><code>        &lt;?php 
  
      $country=$_REQUEST['country'];
      include '../conn.php';
  
      $sql = "SELECT ID,Name FROM Departments Name='$country'";
              $fetched=sqlsrv_query($conn,$sql) ; 
          if( $fetched === false ) { die( print_r( sqlsrv_errors(), true ));}
              while($sno=sqlsrv_fetch_array($fetched,SQLSRV_FETCH_ASSOC))
              {
                  echo $formno=$sno['ID'];
              }
      }
  
      ?&gt;
  </code></pre>
  
  <p><strong>What I have</strong></p>
  
  <p><a href="https://i.stack.imgur.com/UO9M3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UO9M3.png" alt="enter image description here"></a></p>
  
  <p><strong>What I want :</strong> </p>
  
  <p>The ID number of that particular department that is selected in the drop down should display in the text box. I have also attached an extract of what I am trying to do</p>
  
  <p><a href="https://i.stack.imgur.com/cujOA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cujOA.png" alt="enter image description here"></a></p>
  
  <p>But it doesn't seem to work. Where do you think I have gone wrong? Appreciate any help :)       </p>