Home >> FAQs/Tutorials >> PHP Script Tutorials and Tips >> Index

PHP Tutorials - Specifying Input Values for Checkboxes

By: FYICenter.com

(Continued from previous topic...)

How To Specify Input Values for Checkboxes?

Checkboxes can be used in a form for two situations:

  • As a single switch - One <INPUT TYPE=CHECKBOX ...> tag, with no input value specified. When submitted with button pushed down, you will receive a value of "on". When submitted with button not pushed, this field will not be submitted.
  • As a group of multiple selections - Multiple <INPUT TYPE=CHECKBOX ...> tags with the same field name with different input values specified in the "value" attribute. When submitted, input values that associated with checked boxes will be submitted.

The sample PHP script page below is a modified version of submit_comments.php that has one group of multiple checkboxes and single switch:

<?php
  print("<html><form action=processing_forms.php method=post>");
  print("<table><tr><td colspan=2>Please enter and submit your"
    ." comments about FYICenter.com:</td></tr>");
  print("<tr><td>Your Name:</td>"
    ."<td><input type=text name=name></td></tr>\n");
  print("<tr><td>Site Visited:</td><td>"
    ."<input type=checkbox name=site value=dev>Dev FYI Center  "
    ."<input type=checkbox name=site value=sqa>SQA FYI Center  "
    ."<input type=checkbox name=site value=dba>DBA FYI Center  "
    ."</td></tr>\n");
  print("<tr><td>Like Site:</td>"
    ."<td><input type=checkbox name=rate></td></tr>\n");
  print("<tr><td>Comments:</td>"
    ."<td><input type=text name=comment></td></tr>\n");
  print("<tr><td colspan=2><input type=submit><td></tr></table>\n");
  print("</form></html>\n");
?>

If you submit the form with this page, you will get something like this:

Number of values: 4
  name = Peter
  site = dba
  rate = on
  comment = All good sites

But there is a problem with script in processing_forms.php. It picks up only one of the input values selected from the checkbox group. See the next tip for solutions.

(Continued on next topic...)

  1. How To Create a Web Form?
  2. What Are Form Input HTML Tags?
  3. How To Generate a Form?
  4. Where Is the Submitted Form Data Stored?
  5. How To Retrieve the Submitted Form Data?
  6. What Happens If an Expected Input Field Was Not Submitted?
  7. How To Avoid the Undefined Index Error?
  8. How To List All Values of Submitted Fields?
  9. What Are Input Values of SELECT Tags?
  10. How To Specify Input Values for Radio Buttons?
  11. How To Specify Input Values for Checkboxes?
  12. How To Retrieve Input Values for Checkboxes Properly?
  13. How To Supply Default Values for Text Fields?
  14. How To Remove Slashes on Submitted Input Values?
  15. How To Support Multiple Submit Buttons?
  16. How To Support Hidden Form Fields?
  17. How To Generate and Process a Form with the Same Script?
  18. How To Submit Values without Using a Form?
  19. How To Retrieve the Original Query String?
  20. How To Protect Special Characters in Query String?
  21. How To Support Multiple-Page Forms?

Selected Developer Jobs:

More...