How can you display database content only when a button is clicked in PHP?
To display database content only when a button is clicked in PHP, you can use a combination of HTML form and PHP script. You can create a form with a submit button, and upon clicking the button, the form will submit to a PHP script that retrieves and displays the database content. By using the isset() function in PHP to check if the button has been clicked, you can control when the database content is displayed.
<?php
// Check if the button is clicked
if(isset($_POST['display_button'])) {
// Connect to the database and retrieve content
$conn = new mysqli("localhost", "username", "password", "database");
$result = $conn->query("SELECT * FROM table");
// Display the content
while($row = $result->fetch_assoc()) {
echo $row['column_name'] . "<br>";
}
// Close the database connection
$conn->close();
}
?>
<form method="post">
<input type="submit" name="display_button" value="Display Database Content">
</form>
Keywords
Related Questions
- How can array_chunk be used to split a string into word blocks in PHP?
- What are the potential security risks of using SELECT * in SQL queries in PHP?
- What are the best practices for handling character encoding issues when working with CSV files in PHP, especially when dealing with different regional standards like ISO-8859-1 and Windows-1252?