Home > Backend Development > PHP Tutorial > How Can I Use jQuery Ajax to Successfully Retrieve and Display Data from a MySQL Database?

How Can I Use jQuery Ajax to Successfully Retrieve and Display Data from a MySQL Database?

Mary-Kate Olsen
Release: 2024-12-07 00:24:15
Original
935 people have browsed it

How Can I Use jQuery Ajax to Successfully Retrieve and Display Data from a MySQL Database?

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>
Copy after login

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>";
Copy after login

Additional Notes:

  • Ensure that the "connection.php" file contains the MySQL connection details.
  • Replace "samples" and "student" with the name of your database and table, respectively.
  • This code will retrieve all records from the table and display them in an HTML table.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template