title: Changing dropdown value based on AJAX Response
body: <p>I'm not very proficient with AJAX but by all accounts I think this should be working.
  What it's supposed to do is the user selects a name from a dropdown, and another dropdown (populated from a mysql database with PHP) automatically selects the associated object. So let's say you had:</p>
  
  <p>Name: Susie / Michael / Karen</p>
  
  <p>Favorite Fruit: Apple / Orange / Mango / Guava</p>
  
  <p>by selecting Susie, it would autoselect "Orange" since that is her favorite fruit in the database.</p>
  
  <p>I've got this code for the Ajax to change the dropdown to the associated "fruit".</p>
  
  <pre><code> &lt;script type="text/javascript"&gt;
  function getAff(str)
  {
  if (str=="NULL" || str=="")
    {
    document.getElementById("fruit").value="";
    return;
    }
  if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
  else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  xmlhttp.onreadystatechange=function()
    {
    if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200)
      {
      alert(xmlhttp.responseText);
  
   var indexVal = document.getElementById("fruit").length;
    for(x=0; x&lt;indexVal;x++) {
    if (document.getElementById("fruit").options[x].value == xmlhttp.responseText) {
     document.getElementById("fruit").selectedIndex = x;
     //alert(xmlhttp.responseText);
     }
    }
      }
    }
  xmlhttp.open("GET","getaff.php?q="+str,true);
  xmlhttp.send();
  }
  &lt;/script&gt;
  </code></pre>
  
  <p>I've made sure by an alert that it's pulling the right values, though I've commented that out here.</p>
  
  <p>The one snaggle is that the dropdown box is NOT changing. I don't know if that's because it's generated via php? It shouldn't be, though. The value of each dropdown selection is the name of the fruit which is being pulled from the database. I've also alerted to myself the indexvalue amount to see if it's even reading the dropdown, and I'm getting the correct number. But no changing of the dropdown. Any thoughts? Thank you!</p>