Checkbox in PHP
Let's now learn how to work with
checkbox
in PHP. Let's create such a checkbox in our form:
<form action="" method="GET">
<input type="checkbox" name="flag">
<input name="text">
<input type="submit">
</form>
After submitting the form, $_GET
of the checkbox
will contain the string 'on'
if
the checkbox was checked and null
if not:
<?php
var_dump($_GET['flag']); // 'on' or null
?>
Let's display something on the screen depending on whether the checkbox was checked or not:
<?php
if (!empty($_GET)) { // if the form was submitted
if (isset($_GET['flag'])) { // if the checkbox is checked
echo 'checked';
} else {
echo 'not checked';
}
}
?>
Create a form with an input and a checkbox. Using the input, ask the user for their name. After submitting the form, if the checkbox was checked, greet the user, and if it was not checked - say goodbye.