How can PHP and JavaScript be combined to dynamically populate form fields with data from a database upon clicking an icon?

To dynamically populate form fields with data from a database upon clicking an icon, you can use JavaScript to make an AJAX request to a PHP script that fetches the data from the database and returns it to the client side. The JavaScript can then populate the form fields with the retrieved data.

<?php
// Assuming you have a database connection established

// Check if the request is an AJAX request
if(isset($_GET['id']) && isset($_GET['action']) && $_GET['action'] == 'getData') {
    $id = $_GET['id'];
    
    // Query the database to fetch data based on the provided ID
    $query = "SELECT * FROM your_table WHERE id = $id";
    $result = mysqli_query($connection, $query);
    
    // Fetch the data as an associative array
    $data = mysqli_fetch_assoc($result);
    
    // Return the data as JSON
    echo json_encode($data);
    exit;
}
?>