Re: Loading Mysql Data into HTML Table

From: Date: Thu, 19 Dec 2013 14:21:42 +0000
Subject: Re: Loading Mysql Data into HTML Table
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-322579@lists.php.net to get a copy of this message
On 12/19/2013 12:38 AM, 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>'; 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 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!


« previous php.general (#322579) next »