Home >Database >Mysql Tutorial >How can I effectively use jQuery AJAX to retrieve and display data from a MySQL database, addressing issues with deprecated functions and proper response handling?
Using jQuery AJAX to Display Data from MySQL
When working with data in a web application, it's often necessary to retrieve information from a database. JavaScript frameworks like jQuery provide convenient methods for performing Ajax requests, allowing you to fetch data without requiring a full page refresh.
Problem
A user has provided code snippets resembling the following but reports that it's not retrieving data from their MySQL database:
// list.php $(document).ready(function() { var response = ''; $.ajax({ type: "GET", url: "Records.php", async: false, success: function(text) { response = text; } }); alert(response); }); // Records.php <?php $result = mysql_query("select * from users"); $array = mysql_fetch_row($result); ?> <table> <tr> <td>Name:</td> <td>Address:</td> </tr> <?php while ($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>$row[1]</td>"; echo "<td>$row[2]</td>"; echo "</tr>"; } ?> </table>
Solution
The provided code has several issues:
Here's an updated version of the code that addresses these issues:
// list.php <html> <head> <script src="jquery-1.3.2.js"></script> <script> $(document).ready(function() { $("#display").click(function() { $.ajax({ type: "GET", url: "display.php", dataType: "html", success: function(response) { $("#responsecontainer").html(response); } }); }); }); </script> </head> <body> <h3 align="center">Manage Student Details</h3> <table border="1" align="center"> <tr> <td><input type="button">
Additional Notes:
The above is the detailed content of How can I effectively use jQuery AJAX to retrieve and display data from a MySQL database, addressing issues with deprecated functions and proper response handling?. For more information, please follow other related articles on the PHP Chinese website!