What is the difference between using a button and a checkbox in PHP for adding details to an array or defining a variable?
When using a button in PHP to add details to an array or define a variable, the action is triggered by clicking the button, which can only be done once. On the other hand, using a checkbox allows for multiple selections to be made simultaneously. Therefore, if you need to add multiple details to an array or define multiple variables at once, using checkboxes would be more suitable.
// Using checkboxes to add details to an array
if(isset($_POST['submit'])){
$selectedOptions = $_POST['options'];
$detailsArray = [];
foreach($selectedOptions as $option){
$detailsArray[] = $option;
}
print_r($detailsArray);
}
<form method="post">
<input type="checkbox" name="options[]" value="Option 1"> Option 1
<input type="checkbox" name="options[]" value="Option 2"> Option 2
<input type="checkbox" name="options[]" value="Option 3"> Option 3
<input type="submit" name="submit" value="Submit">
</form>