How can PHP handle multiple form inputs with the same name using arrays?
When handling multiple form inputs with the same name in PHP, you can use arrays to capture all the values. By appending "[]" to the input name in the HTML form, PHP will automatically create an array with all the values submitted. This allows you to loop through the array and access each individual value.
<form method="post">
<input type="text" name="input[]" />
<input type="text" name="input[]" />
<input type="text" name="input[]" />
<input type="submit" />
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$inputs = $_POST["input"];
foreach($inputs as $input) {
echo $input . "<br>";
}
}
?>
Keywords
Related Questions
- What are the best practices for normalizing database tables for PHP applications?
- How can you debug a PHP script in NetBeans to see the execution line by line?
- In what scenarios would it be more effective to modify the original source content with print CSS rather than using JavaScript within an iframe?