title: Fetching data from mysql through anchor tag onclick event using ajax
body: <p>I would like to be able to display data from mysql in a table on same page using ajax. When user clicks the anchor link with value=seatid the table displays the table showing the name using the seatid. But i can't get to display the name, when i click the link it just display the table with the Header "Name" but not the actual name of the person seating on that seatid.. I'm using php, javascript, ajax, and mysql... Is there something wrong with the code, pls help me... </p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Seat Mapper System&lt;/title&gt;
  &lt;style&gt;
      *{
       font-size: 16px;
       color: black;
       font-family: tahoma;}
  
      td td{
      width: 75px;
      height: 35px;
      }
  
      td td a{
      text-decoration: none;
      display: block;
      width: 100%;
      height: 100%;
  }
  &lt;/style&gt;
  &lt;script&gt;
  function showInformation(str){
      if (str==""){
      document.getElementById("txtInfo").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("txtInfo").innerHTML=xmlhttp.responseText;
          }
      }
  xmlhttp.open("GET","getinfo.php?q="+str,true);
  xmlhttp.send();
  }
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="txtInfo"&gt;&lt;b&gt;Person info will be listed here.&lt;/b&gt;&lt;/div&gt;
  &lt;?php
  $linkID = @ mysql_connect("localhost", "root", "*******") or die("Could
  not connect to MySQL server");
  @ mysql_select_db("seatmapping") or die("Could not select database");
  /* Create and execute query. */
  $query = "SELECT * from seats order by rowId, columnId desc";
  $result = mysql_query($query);
  $prevRowId = null;
  $seatColor = null;
  $tableRow = false;
  //echo $result;
  echo "&lt;table width='100%' border='0' cellpadding='0' cellspacing='0'&gt;";
  while(list($rowId,$columnId,$status,$updatedby,$name,$seatid)=mysql_fetch_row($result))
  {
  if ($prevRowId != $rowId) {
      if ($rowId != 'A') {
          echo "&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;";
          echo "\n&lt;/tr&gt;";
          }
      $prevRowId = $rowId;
      echo "\n&lt;tr&gt;&lt;td align='center'&gt;;
              echo&lt;table border='0' cellpadding='0' cellspacing='3'&gt;&lt;tr&gt;";
  } 
      else {
      $tableRow = false;
  }
  if ($status == 0) {
      $seatColor = "99FF33";
  } else if ($status == 1 &amp;&amp; $updatedby == 'user1') {
      $seatColor = "FFCC99";
  } else if ($status == 2 &amp;&amp; $updatedby == 'user1') {
      $seatColor = "FF9999";
  } else {
      $seatColor = "red";
  }
  
  echo "\n&lt;td bgcolor='$seatColor' align='center'&gt;";
  echo "&lt;a href=\"#\" onclick=\"showInformation(this)\" value=\"$seatid\"&gt;&lt;b&gt;$seatid&lt;/b&gt;&lt;/a&gt;";
  echo "&lt;/td&gt;";
      if (($rowId == 'A' &amp;&amp; $columnId == 7) || ($rowId == 'B' &amp;&amp; $columnId == 7))
      {
          // This fragment is for adding a blank cell which represent the "center aisle"
          echo "&lt;td&gt;&amp;nbsp;&lt;/td&gt;";
      }
  }
  echo "&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;";
  echo "&lt;/tr&gt;";
  echo "&lt;/table&gt;";
  
  /* Close connection to database server. */
  mysql_close();
  ?&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>This is the getinfo.php page:</p>
  
  <pre><code>&lt;?php
  $q = intval($_GET['q']);
  
  $con = mysql_connect('localhost','root','Newpass123#','seatmapping');
  if (!$con)
    {
    die('Could not connect: ' . mysql_error($con));
    }
  
  mysql_select_db($con,'seatmapping');
  $sql="SELECT name, seatid FROM seats WHERE seatid = '".$q."'";
  
  $result = mysql_query($con,$sql);
  
  echo "&lt;table border='1'&gt;
  &lt;tr&gt;
  &lt;th&gt;Name&lt;/th&gt;
  &lt;th&gt;Seat Number&lt;/th&gt;
  &lt;/tr&gt;";
  
  while($row = mysql_fetch_array($result))
    {
    echo "&lt;tr&gt;";
    echo "&lt;td&gt;" . $row['name'] . "&lt;/td&gt;";
    echo "&lt;td&gt;" . $row['seatid'] . "&lt;/td&gt;";
    echo "&lt;/tr&gt;";
    }
  echo "&lt;/table&gt;";
  
  mysql_close($con);
  ?&gt; 
  </code></pre>