How can PHP be used to display search results from a database directly below input fields on a form?
To display search results from a database directly below input fields on a form, you can use PHP to retrieve the search query from the form submission, query the database for matching results, and then display the results below the form. This can be achieved by embedding the PHP code within the HTML form structure to seamlessly display the search results on the same page.
<?php
// Assuming you have already established a database connection
if(isset($_POST['search'])) {
$search = $_POST['search'];
$query = "SELECT * FROM your_table WHERE column_name LIKE '%$search%'";
$result = mysqli_query($connection, $query);
echo "<h2>Search Results:</h2>";
while($row = mysqli_fetch_assoc($result)) {
echo "<p>{$row['column_name']}</p>";
}
}
?>
<form method="post" action="">
<input type="text" name="search" placeholder="Search...">
<input type="submit" value="Search">
</form>
Keywords
Related Questions
- What is the significance of using empty brackets [] when adding elements to an array in PHP loops?
- What is the recommended method to handle special characters, such as umlauts, in email subjects and senders when using PHP?
- How can the behavior of different browsers impact the display of PHP-generated content?