title: Ajax-MySql-Php issue (submit text from form, send via AJAX to php script, back to div)
body: <p>I would like to send data to a MySql DB from a form via AJAX and a PHP script. I can get it perfect using option selection in the form, but I simply cannot get it using a text submit form.</p>
  
  <p>First what works:</p>
  
  <h1>HTML Form:</h1>
  
  <pre><code>&lt;form&gt;
  &lt;select name="users" onchange="showUser(this.value)"&gt;
  &lt;option value=""&gt;Select a person:&lt;/option&gt;
  &lt;option value="1"&gt;Peter Griffin&lt;/option&gt;
  &lt;option value="2"&gt;Lois Griffin&lt;/option&gt;
  &lt;/select&gt;
  &lt;/form&gt;
  </code></pre>
  
  <h1>.js Scipt:</h1>
  
  <pre><code>function showUser(str)
  {
          if (str=="")
    {
    document.getElementById("txtHint").innerHTML="";
    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("txtHint").innerHTML=xmlhttp.responseText;
      }
    }
  
  xmlhttp.open("GET","getuser.php?q="+str,true);
  xmlhttp.send();
  }
  </code></pre>
  
  <h1>PHP Script:</h1>
  
  <pre><code>&lt;?php
  $q=$_GET['q'];
  
  $con = mysql_connect('localhost', 'root');
  if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }
  
  mysql_select_db("ajax_demo", $con);
  
  $sql="SELECT * FROM emp WHERE id = '$q'";
  
  $result = mysql_query($sql);
  
  $i = 0;
  
  echo "&lt;table class=\"table-master\"&gt;
  &lt;tr&gt;
  &lt;th&gt;Firstname&lt;/th&gt;
  &lt;th&gt;Lastname&lt;/th&gt;
  &lt;th&gt;Age&lt;/th&gt;
  &lt;th&gt;Hometown&lt;/th&gt;
  &lt;th&gt;Job&lt;/th&gt;
  &lt;/tr&gt;";
  
  while($row = mysql_fetch_array($result))
    {
    $i++;
    echo "&lt;tr class=\"d".($i &amp; 1)."\"&gt;";
    echo "&lt;td&gt;" . $row['FirstName'] . "&lt;/td&gt;";
    echo "&lt;td&gt;" . $row['LastName'] . "&lt;/td&gt;";
    echo "&lt;td&gt;" . $row['Age'] . "&lt;/td&gt;";
    echo "&lt;td&gt;" . $row['Hometown'] . "&lt;/td&gt;";
    echo "&lt;td&gt;" . $row['Job'] . "&lt;/td&gt;";
    echo "&lt;/tr&gt;";
    }
  echo "&lt;/table&gt;";
  
  mysql_close($con);
  ?&gt;
  </code></pre>
  
  <p>What I want to do is to have a text field in my HTML which will be filled out by the user when the user hits 'submit', that information gets passed into the js script containing the AJAX segment, gets queried by the DB and the results are outputed into my DIV. </p>
  
  <p>Where I think I am going wrong - cannot use <em>onchange</em> obviously in the form when using a text form for submition. Has to be onsubmit !? The js script works so therefore I think my AJAX part is fine. The PHP script also works but again, everything breaks down as soon as I try to get text input from the form. Perhaps my js script is not set up to capture the incoming text form variables!? Any help / guidance would be appreciated.</p>