How can PHP be used to dynamically add and display multiple selected items in a growing table within a form?
To dynamically add and display multiple selected items in a growing table within a form using PHP, we can utilize JavaScript to handle the dynamic addition of rows in the table based on user input. PHP can be used to process the form data and generate the table rows with the selected items.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$selectedItems = $_POST['selected_items'];
echo "<table>";
foreach ($selectedItems as $item) {
echo "<tr><td>$item</td></tr>";
}
echo "</table>";
}
?>
<form method="post">
<select name="selected_items[]" multiple>
<option value="Item 1">Item 1</option>
<option value="Item 2">Item 2</option>
<option value="Item 3">Item 3</option>
</select>
<input type="submit" value="Submit">
</form>