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

PHP Script Tips - Processing Web Forms

By: FYICenter.com

Part:   1  2  3   4  5  6  7  8 

(Continued from previous part...)

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

Number of values: 4
  name = Joe
  job = Developer
  rate = 3
  comment = I like it.

How To Specify Input Values for Radio Buttons?

Radio buttons can be used in a form for two situations:

  • As a single switch - One <INPUT TYPE=RADIO ...> 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 exclusive selections - Multiple <INPUT TYPE=RADIO ...> tags with the same field name with different input values specified in the "value" attribute. When submitted, only one input value that associated with pushed button will be submitted.

The sample PHP script page below is a modified version of submit_comments.php that has one group of exclusive radio buttons named as "job" and single switch named as "rate":

<?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>Your Job Title:</td>"
    ."<td><input type=radio name=job value=dev>Developer  "
    ."<input type=radio name=job value=sqa>QA Engineer  "
    ."<input type=radio name=job value=dba>DBA  "
    ."<input type=radio name=job value=other>Other  "
    ."</td></tr>\n");
  print("<tr><td>Like Site:</td>"
    ."<td><input type=radio 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 = Sue
  job = sqa
  rate = on
  comment = Will visit FYICenter.com again.

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 part...)

Part:   1  2  3   4  5  6  7  8 


Selected Developer Jobs:

More...