Home >Backend Development >PHP Tutorial >How to Retrieve and Display MySQL Data Using jQuery Ajax?

How to Retrieve and Display MySQL Data Using jQuery Ajax?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-02 07:15:11898browse

How to Retrieve and Display MySQL Data Using jQuery Ajax?

Retrieving Data from MySQL using jQuery Ajax

Your code appears to have an issue with retrieving data from the MySQL table. To resolve this, you should consider the following:

jQuery Ajax Code

Your Ajax code can be improved by structuring it more appropriately. Consider using the following:

<script type="text/javascript">
$(document).ready(function() {

    $("#display_records").click(function() {

        $.ajax({
            type: "GET",
            url: "Records.php",
            dataType: "html",
            success: function(response) {
                $("#response_container").html(response);
            }
        });

    });

});
</script>

In the code above, we have added an id to the button for clarity, and we are targeting the HTML element with id "response_container" to display the output.

MySQL Connection and Query

For MySQL database interactions, you should utilize the following for mysqli connectivity:

<?php
$con = mysqli_connect("localhost", "root", "", "simple_ajax");

And to retrieve data from the "users" table:

$result = mysqli_query($con, "SELECT * FROM users");

Displaying Data

To display the retrieved data, modify your code as follows:

<table>

By incorporating these modifications, your code should successfully retrieve data from the MySQL table and display it in an HTML table.

The above is the detailed content of How to Retrieve and Display MySQL Data Using jQuery Ajax?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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