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.

Tips & Warnings

  • PHP code must be contained within "<?php" and "?>" tags.

Related Searches:

References

Resources

Comments

You May Also Like

  • PHP Tutorial on Record Processing

    PHP can be used to process data for business applications such as inventory control programs and online cash registers. For these applications,...

  • Tutorial for Dreamweaver Check Box PHP

    Dreamweaver is a Web-authoring application by Adobe that allows you to create professional and dynamic websites. Different programming and scripting languages can...

  • How to Insert a Checkbox in PHP MySQL

    PHP can select records from a MySQL database, and then provide a way to choose selected records using an HTML check box....

  • HTML Tutorial for a Checkbox Form

    An HTML form allows the users of your website to submit data to your email or database. One of the possible components...

Related Ads

Featured