How can PHP be used to dynamically generate HTML tables based on user input from a form?
To dynamically generate HTML tables based on user input from a form using PHP, you can capture the form data using $_POST or $_GET superglobals, process the input, and then generate the HTML table structure with the user-provided data.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve user input from the form
$rows = $_POST["rows"];
$columns = $_POST["columns"];
// Generate HTML table based on user input
echo "<table border='1'>";
for ($i = 1; $i <= $rows; $i++) {
echo "<tr>";
for ($j = 1; $j <= $columns; $j++) {
echo "<td>Row $i, Column $j</td>";
}
echo "</tr>";
}
echo "</table>";
}
?>
<form method="post">
<label for="rows">Number of Rows:</label>
<input type="number" name="rows" id="rows" required>
<label for="columns">Number of Columns:</label>
<input type="number" name="columns" id="columns" required>
<input type="submit" value="Generate Table">
</form>
Related questions
-
PHP4 syntax compatibility functionality
-
PHP SQL MySQL
-
PHP optional attributes HTML elements