I\'m building a form using HTML with JQuery mobile so that the form can be used on mobile devices.
I have the form exporting to CSV via email. However the write to t
foreach($_POST['checkbox'] as $value)
{
echo 'checked: '.$value.'';
}
Change your HTML and give a name
attribute to all your checkboxes, like so:
<form action="" method="post">
<label for="question4" class="input"> 4. What language (s) are you currently using? </label>
<fieldset data-role="controlgroup">
<legend>Multi select:</legend>
<input type="checkbox" name="checkbox[]" id="checkbox-1" class="custom" value="english"/>
<label for="checkbox-1"> English</label>
<input type="checkbox" name="checkbox[]" id="checkbox-2" class="custom" />
<label for="checkbox-2">French</label>
<input type="checkbox" name="checkbox[]" id="checkbox-3" class="custom" />
<label for="checkbox-3">Spanish</label>
<input type="checkbox" name="checkbox[]" id="checkbox-4" class="custom" />
<label for="checkbox-4">Brazilian Portuguese</label>
<input type="checkbox" name="checkbox[]" id="checkbox-5" class="custom" />
<label for="checkbox-5">Italian</label>
<input type="checkbox" name="checkbox[]" id="checkbox-6" class="custom" />
<label for="checkbox-6">German</label>
<input type="checkbox" name="checkbox[]" id="checkbox-7" class="custom" />
<label for="checkbox-7">Japanese</label>
</fieldset>
</br>
<input type="submit" name="submit">
</form>
Once the form has been submitted, the name attributes will be passed as array and they'll be accessible using the same variable. Now, you can use isset()
to check if a particular item was set:
if(isset($_POST['checkbox'])) {
print_r($_POST); //print all checked elements
}
If you want to get the number of checkboxes that were checked, you can use count()
:
$number = count($_POST['checkbox']);
Note: currently, only the first element has a value
attribute. You'll have to add it for the rest of the checkboxes for it to work properly.
Hope this helps!
Have a quick look at this answer for checking if a checkbox is checked.
How to check whether a checkbox is checked in jQuery?
But basically you want to do something like below to check its value:
if ($("#element").is(":checked")) {
alert("I'm checked");
}