I am doing a PHP assignment where I am getting input that uses some checkboxes and stores the information in a file.
Let's say I have a checkbox
Active: <input type="checkbox" name="preference" value="Pepperoni" checked="checked>
When I say something like:
$Preference1 = $_POST['preference'];
What is stored in $Preference1
? Does it store information that tells whether the box was checked or not, or does it contain "Pepperoni"
? Is there a way to check to see if a box is checked? Like,
Pesudocode
if Preference1 is checked
add Pepperoni to the file;
if Preference2 is checked
add Sausage to the file;
I did work with radio buttons but those are a bit different since only one of those can be checked at a time, but for checkboxes, multiples boxes and be selected. Is there a way to see if a button is checked and if so, how would I do that? Thanks.
UPDATE After doing some digging around I found a post that did something like this:
$('#isAgeSelected').attr('checked')
But that is using jQuery. Would something like the .attr
work in PHP? Could I say something like
if($_POST['preference'].attr("checked"))
//do whatever
Does this work in PHP?