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;
}
?>
Keywords
Related Questions
- When should one use the functions str_pos + substr versus preg_match_all for extracting substrings in PHP?
- Are there any specific pitfalls to be aware of when using cloud-based IDEs like Cloud9 for PHP development?
- How can the code snippet provided be optimized to handle a larger number of entries in the guestbook?