How To Select Data From Database Using Ajax In PHP

The finest example using ajax, PHP, and MySQL is what I’ve presented. Therefore, you can learn how to view all data using Ajax in PHP and MySQL. Simple explanations are provided for each stage.

  1. You need to create an HTML form that will allow the user to input the data they want to select. The form should contain inputs for the data you want to select, as well as a submit button.
  2. You need to create a database table to store the data you want to select. You can do this using SQL commands in a database management tool like PHPMyAdmin.
  3. When the user submits the form, you need to use JavaScript to send the data to the server using an Ajax request. The Ajax request should be sent to a PHP script that will handle the selection.
  4. In the PHP script, you need to extract the data from the Ajax request and use an SQL SELECT statement to select the corresponding data from the database.

HTML From:

<!DOCTYPE html>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <button id="showdata">Show All User Data</button>
  <div id="all-data-user-table"></div>


JavaScript/Ajax code:

    jQuery('#showdata').on('click', function(e) {
        url: "view.php",
        type: "POST",
        dataType: "html",   
        success: function(data){


PHP code:

$conn = mysqli_connect("localhost", "root", "", "database");
    $result = mysqli_query($conn,"SELECT * FROM table_name ");
    <table class="all-record">
                <th>First Name</th>
            if(mysqli_num_rows($result) == 0){ 
                <tr><th colspan="4"><?php echo "<p>No results found<p>"; ?></th></tr>
                while( $row = mysqli_fetch_assoc( $result ) ){
                    $id = $row['id'];
                        <td><?php echo $row['id']?></td>
                        <td><?php echo $row['name']?></td>
                        <td><?php echo $row['email']?></td>
                            <span class='edit-all-data' data-id='<?php echo $id; ?>'>Edit</span>
                            <span class='delete-all-data' data-id='<?php echo $id; ?>'>Delete</span>

Submit a Comment

Your email address will not be published. Required fields are marked *


Select Categories