How can dynamic input fields be added to a form using PHP to allow users to enter multiple values?
To allow users to enter multiple values in a form using PHP, dynamic input fields can be added. This can be achieved by using JavaScript to dynamically add input fields when the user clicks a button. The PHP code will then handle processing the form data, including the multiple values entered by the user.
<form method="post" action="process_form.php">
<div id="input_fields">
<input type="text" name="value[]">
</div>
<button type="button" onclick="addInputField()">Add Field</button>
<input type="submit" value="Submit">
</form>
<script>
function addInputField() {
var input = document.createElement("input");
input.type = "text";
input.name = "value[]";
document.getElementById("input_fields").appendChild(input);
}
</script>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$values = $_POST["value"];
foreach ($values as $value) {
// Process each value as needed
echo "Value: " . $value . "<br>";
}
}
?>