How to Process a Checkbox With PHP
You can use PHP to process the value of a checkbox after it has been clicked or not in an HTML form. This is helpful when you want to let your Web page visitor select one or more options from a list or to answer yes/no type of questions. If a checkbox is clicked, its value is set in the "$_POST" array, which is sent to the Web page that processes the submitted form.
Instructions
-
-
1
Open the HTML file that you want to use a checkbox form in. Use a text editor such as Windows Notepad.
-
2
Create an HTML form with an input type of "checkbox". Include a "Submit" button for the user to click after she's made her selection. For example, "<form action="mypage.php" method="post">text of question<input type="checkbox" name="myvariable" value="Yes"/><input type="submit" name="myform" value="Submit"/></form>" stores the checkbox status in the "myvariable" variable and redirects the user to the Web page "mypage.php" when the "Submit" button is clicked.
-
-
3
Save and close the HTML file.
-
4
Open your PHP file.
-
5
Process the checkbox by using the "isset" function to check if a value for the checkbox was set in the "$_POST" array. If the checkbox was clicked, then the form's variable will have a value equal to what the "value=" was set to in the form. If the checkbox was unchecked, then the variable won't be set and "isset" will return false. For example, "<?php if(isset($_POST['myvariable'])){ $myvalue = $_POST['myvariable']; } else { $myvalue = "checkbox not checked"; } ?>".
-
6
Save the PHP file.
-
1
Tips & Warnings
PHP code must be contained within "<?php" and "?>" tags.