Discussion:
Loading Mysql Data into HTML Table
Adarsh Sharma
2013-12-19 05:38:08 UTC
Permalink
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','***@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
Jim Giner
2013-12-19 14:21:42 UTC
Permalink
Post by Adarsh Sharma
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']);
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
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
Try running your ajax php as a standalone from the address bar so you
can monitor it closely (add some echos to it). Get the ajax portion
debugged first!
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jim Lucas
2013-12-19 16:49:25 UTC
Permalink
Post by Adarsh Sharma
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']);
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>';
The following three lines will give you parse errors. Remove the extra double
quotes
Post by Adarsh Sharma
echo '<td>'$row['first_name']'</td>';
echo "<td>"$row['last_name'] "</td>";
echo "<td>" $row['create_time']"</td>";
echo "<td>{$row['first_name']}</td>";
echo "<td>{$row['last_name']}</td>";
echo "<td>{$row['create_time']}</td>";

Now browse to the getuser.php?q=10 and verify that the output looks correct.
Once you have that nailed, try calling it from your other html file.
Post by Adarsh Sharma
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
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
--
Jim Lucas

http://www.cmsws.com/
http://www.cmsws.com/examples/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
tamouse pontiki
2013-12-21 04:35:00 UTC
Permalink
Simple thing to do, lint your php:

$ php -l getuser.php
Parse error: parse error, expecting `','' or `';'' in getuser.php on line 27
Errors parsing getuser.php

Loading...