How can one implement a script to select multiple checkboxes at once in PHP?
To implement a script to select multiple checkboxes at once in PHP, you can use an array in the name attribute of the checkboxes. This way, when the form is submitted, you can loop through the array to get all the selected checkboxes. Below is a sample PHP code snippet that demonstrates this:
<form method="post" action="">
<input type="checkbox" name="checkboxes[]" value="option1"> Option 1
<input type="checkbox" name="checkboxes[]" value="option2"> Option 2
<input type="checkbox" name="checkboxes[]" value="option3"> Option 3
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['checkboxes'])) {
foreach($_POST['checkboxes'] as $selected) {
echo $selected."</br>";
}
}
}
?>