title: How can I query a mysql database using jquery and ajax?
body: <p>I have a form with a "select" box. When the box is selected, I want the appropriate records from the database to be shown on the same page.</p>
  
  <p>There are two files involved: an HTML page that contains the form:</p>
  
  <pre><code>&lt;form id="theForm"&gt;
  &lt;select id="theDropdown"&gt;
  &lt;option value="biology"&gt;Biology&lt;/option&gt;
  &lt;option value="chemistry"&gt;Chemistry&lt;/option&gt;
  &lt;option value="english"&gt;English&lt;/option&gt;
  &lt;/select&gt;
  &lt;/form&gt;
  &lt;div id="resultsGoHere"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>and also contains the jQuery code:</p>
  
  <pre><code>&lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
  $(document).ready(function() {
  
      $('#theDropdown').on('change', function() {
         var qString = '{sub: ' + $('#theDropdown option:selected').text() + '}';
         $.post('sub_db_handler.php', qString, processResponse);
         // $('#resultsGoHere').html(qString);
      });
  
      function processResponse(data) {
          $('#resultsGoHere').html(data);
      }
  
  });
  &lt;/script&gt;
  </code></pre>
  
  <p>The jQuery code seems to successfully grab the selected value of the select menu and formats a JSON query string, which is printed out if the commented line above is uncommented.</p>
  
  <p>Here is the PHP script that is referred to in the <code>post</code> command above.</p>
  
  <pre><code>&lt;?php
  
  $con = mysql_connect("localhost","rongilmo_ron","******");
  if(!$con) { die('Could not connect: ' . mysql_error()); }
  mysql_select_db("rongilmo_databases", $con);
  $sub = $_POST['theDropdown'];
  
  $q = "SELECT dbs.db_name, dbs.db_url FROM dbs, subjects, subjects_databases
  WHERE subjects.subject_id=subjects_databases.subject_id
  AND subjects_databases.database_id=dbs.db_id
  AND subjects.subject_name='$sub'";
  
  $r = mysql_query($q);
  $array = mysql_fetch_row($r);
  echo json_encode($array);
  
  ?&gt;
  </code></pre>
  
  <p>I get no results at all. I've tested the query in non-ajax mode, so that isn't the problem.</p>
  
  <p>Still new to ajax.  I've been working on this for two days and can't seem to make it work, despite reading lots of tutorials and doing a lot of googling.</p>
  
  <p>Any help you can offer will be greatly appreciated.</p>