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
- Are there any specific PHP functions or methods recommended for handling image sizes on a website?
- What resources or tutorials would you recommend for PHP beginners looking to enhance their knowledge and skills for handling data manipulation in WordPress?
- What is the best practice for displaying a user's comments on a website using PHP and MySQL?