title: Ajax, PHP, Javascript to Pull Values from Database after Combo Box Selection
body: <p>I have a page with Ajax, PHP, and Javascript. On that page, I have a selection/combo box of websites. When the user selects a website, I have it calling a javascript function which uses Ajax and the GET method to send to a PHP file and execute the PHP code. That PHP file, I have connecting to a database (using the users website selection to pull a specific row). I want to then send some details stored in the database (about the website the user selected) and use them on the page to run some other PHP code.</p>
  
  <p>I can't seem to get it to work with the code I have and I've tried everything I can think of.</p>
  
  <p><strong>Selection box code calling javascript function:</strong></p>
  
  <pre><code>&lt;select name="website" onchange="doSomething(this.value)" /&gt;
  &lt;option&gt;option 1&lt;/option&gt;
  &lt;option&gt;option 2&lt;/option&gt;
  &lt;option&gt;etc.&lt;/option&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p><strong>Javascript function using Ajax to call a PHP file:</strong></p>
  
  <pre><code>function doSomething(str)
  {
  if (str == "")
  {
   document.getElementById("DIVdisplayID").innerHTML="Nothing Selected";
   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)
  {
      document.getElementById("DIVdisplayID").innerHTML=xmlhttp.responseText;
  }
  }
  xmlhttp.open("GET","URLHERETOPHPFILEHERE?website="+str,true);
  xmlhttp.send();
  
  }
  </code></pre>
  
  <p><strong>My Display DIV:</strong></p>
  
  <pre><code>&lt;div id="DIVdisplayID"&gt;&lt;/div&gt;
  </code></pre>
  
  <p><strong>PHP File to access database info based off the website the user selected:</strong></p>
  
  <pre><code>&lt;?php
  $website=$_GET["q"];
  
  $con = mysql_connect("localhost","username","password");
  if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  
  mysql_select_db("database", $con);
  
  $result = mysql_query("SELECT * FROM Websites
  WHERE WebsiteURL='$website'");
  
  while($row = mysql_fetch_array($result))
  {
  echo $row['WebsiteDetail1'] . " " . $row['WebsiteDetail2'];
  echo "&lt;br /&gt;";
  }
  
  mysql_close($con);
  ?&gt;
  </code></pre>