How can data be displayed in a table using a dropdown menu in PHP?
To display data in a table using a dropdown menu in PHP, you can create an HTML form with a dropdown menu that allows the user to select a value. Then, based on the selected value, you can query the database and display the data in a table format. This can be achieved by using PHP to handle the form submission, retrieve the selected value, and generate the table with the corresponding data.
<?php
// Connect to database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Handle form submission
if(isset($_POST['submit'])){
$selected_value = $_POST['dropdown'];
// Query database based on selected value
$sql = "SELECT * FROM table WHERE column = '$selected_value'";
$result = $conn->query($sql);
// Display data in table format
echo "<table border='1'>";
while($row = $result->fetch_assoc()) {
echo "<tr>";
foreach($row as $value) {
echo "<td>".$value."</td>";
}
echo "</tr>";
}
echo "</table>";
}
// Close database connection
$conn->close();
?>
<form method="post">
<select name="dropdown">
<option value="value1">Value 1</option>
<option value="value2">Value 2</option>
<option value="value3">Value 3</option>
</select>
<input type="submit" name="submit" value="Submit">
</form>
Keywords
Related Questions
- Are there any recommended best practices for implementing a script to resize images in PHP?
- How can the issue of data not being received by the webservice be troubleshooted when making a POST request to an IIS 5.0 server in PHP?
- What potential issues or errors might arise when manipulating variables in PHP?