Loading Mysql Data into HTML Table

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

I am building a small dashboard in which i want to show some data from
mysql database in UI.

I am using AJAX with PHP for this. Below are my index.html and getuser.php
:-

----------- Index.html -----------

<html>
<head>
<script>
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 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="10">rahul.sharma</option>
<option value="168">adarsh.sharma</option>
<option value="78">smya.ranjan</option>
<option value="239">akay.rawat</option>
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html>

---- getuser.php ----

<?php
$q = intval($_GET['q']);

$con = mysqli_connect('localhost','root','root@123');
if (!$con)
  {
  die('Could not connect: ' . mysqli_error($con));
  }

mysqli_select_db('otrs',$con);

$sql="SELECT first_name,last_name,create_time FROM users WHERE id =
'".$q."'";

$result = mysqli_query($con,$sql);

echo "<table border='5'>
<tr>
<th>first_name</th>
<th>last_name</th>
<th>create_time</th>
</tr>";

while($row = mysqli_fetch_row($result))
  {
  echo '<tr>';
  echo '<td>'$row['first_name']'</td>';
  echo "<td>"$row['last_name'] "</td>";
  echo "<td>" $row['create_time']"</td>";
  echo "</tr>";
  }
echo "</table>";


mysqli_close($con);
?>

--- Program is running fine when i opened http:/localhost/getuser

but when i choose one user then following message is displayed in browser
instead of DB values :

Select a person:rishit.shettyadarsh.sharmasoumya.ranjanakshay.rawat
first_name last_name create_time "; while($row = mysqli_fetch_row($result))
{ echo ''; echo ''$row['first_name']''; echo ""$row['last_name'] ""; echo
"" $row['create_time']""; echo ""; } echo ""; mysqli_close($con); ?> ~

DB connectivity is fine. I think something is wrong in getuser.php. Anyone
has any context on this or faced simliar issue before. Please let me know.


Thansk

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux