On 12/18/2013 09:38 PM, Adarsh Sharma wrote:
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>';
The following three lines will give you parse errors. Remove the extra double
quotes
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.
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 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