Using jQuery Ajax to Retrieve Data from MySQL
Problem:
The provided jQuery Ajax code is unable to retrieve and display data from a MySQL table successfully.
Solution:
To effectively retrieve data from a MySQL table using jQuery Ajax, several modifications are necessary:
jQuery Ajax Code:
<script type="text/javascript" src="jquery-1.3.2.js"></script> <script type="text/javascript"> $(document).ready(function() { $("#display").click(function() { $.ajax({ //create an ajax request to display.php type: "GET", url: "display.php", dataType: "html", //expect html to be returned success: function(response){ $("#responsecontainer").html(response); //alert(response); } }); }); }); </script>
Display.php (PHP Code to Retrieve Data):
include("connection.php"); mysqli_select_db("samples",$con); $result=mysqli_query("select * from student",$con); echo "<table border='1' > <tr > <td align=center> <b>Roll No</b></td> <td align=center><b>Name</b></td> <td align=center><b>Address</b></td> <td align=center><b>Stream</b></td> <td align=center><b>Status</b></td>"; while($data = mysqli_fetch_row($result)) { echo "<tr>"; echo "<td align=center>$data[0]</td>"; echo "<td align=center>$data[1]</td>"; echo "<td align=center>$data[2]</td>"; echo "<td align=center>$data[3]</td>"; echo "<td align=center>$data[4]</td>"; echo "</tr>"; } echo "</table>";
Additional Notes:
The above is the detailed content of How Can I Use jQuery Ajax to Successfully Retrieve and Display Data from a MySQL Database?. For more information, please follow other related articles on the PHP Chinese website!