A Sticky Form Working with All of the Form Controls

 

Introduction.  Remember that we have several standard form controls.
  • text boxes
    • password
  • text areas
  • check boxes
  • radio buttons
  • select/option boxes

I think it is important that we work with sticky forms that contain all of these controls since you never know which or how many of them you will need on a form.  Fortunately, with basically no help from Ullman or Lerdorf, I figured out a way to do this that is quite analogous to how they can be done in ASPs and JSPs.

In this vein we will work one more example.  Then I will focus on the idiosyncrasies of working with each type of control.

You should call this script all_form_sticky.php.  This code will not provide much in the way of input validation in order to help focus on the current issues.  Notice that all of the PHP is embedded within HTML form tags and highlighted in blue.

 

<html>
<head>
<title>HTML Form</title>
</head>

<body bgcolor = "660000" text="cccccc">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table>
<tr>
<td><font size = 4 color=cccccc>First Name:</font>
</td>
<td><input type=text name="txt_first_name" size=20 value = "<?php if (isset($_REQUEST['txt_first_name'])) echo $_REQUEST['txt_first_name']; ?>">
</td>
</tr>
<tr>
<td><font size = 4 color=cccccc>Last Name:</font>
</td>
<td><input type=text name="txt_last_name" size=20 value = "<?php if (isset($_REQUEST['txt_last_name'])) echo $_REQUEST['txt_last_name']; ?>">
</td>
</tr>
<tr>
<td><font size = 4 color=cccccc>EMail Address:</font>
</td>
<td><input type=text name="txt_email" size=50 value = "<?php if (isset($_REQUEST['txt_email'])) echo $_REQUEST['txt_email']; ?>">
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
</tr>
<tr>
<td><font size = 4>Comments:</font>
</td>
<td><textarea name="tarea_comments" rows = 5 columns = 20>
<?php if (isset($_REQUEST['tarea_comments'])) echo $_REQUEST['tarea_comments']; ?>
</textarea>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
</tr>
<tr>
<td><font size = 4 color=cccccc>Interests:</font>
</td>
<td><input type=checkbox name="chk_php" value="true"
<?php if (($_REQUEST['chk_php']) == 'true' ) echo 'checked'; ?>>
<font size = 4 color=cccccc>PHP</font>
</td>
</tr>
<tr>
<td>
</td>
<td><input type=checkbox name="chk_jsp" value="true"
<?php if (($_REQUEST['chk_jsp']) == 'true' ) echo 'checked'; ?>>
<font size = 4 color=cccccc>JSP</font>
</td>
</tr>
<tr>
<td>
</td>
<td><input type=checkbox name="chk_mysql" value="true"
<?php if (($_REQUEST['chk_mysql']) == 'true' ) echo 'checked'; ?>>
<font size = 4 color=cccccc>MySQL</font>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
</tr>
<tr>
<td><font size = 4 color=cccccc>Credit Card:</font>
</td>
<td><select name="sel_credit_card">
<option value="Discover" <?php if (($_REQUEST['sel_credit_card']) == 'Discover') echo 'selected'; ?>>Discover
<option value="MasterCard" <?php if (($_REQUEST['sel_credit_card']) == 'MasterCard') echo 'selected'; ?>>Mastercard
<option value="Visa" <?php if (($_REQUEST['sel_credit_card']) == 'Visa') echo 'selected'; ?>>Visa
</select>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
</tr>
<tr>
<td><font size = 4 color=cccccc>Education:</font>
</td>
<td><input type=radio name="rb_education" value="NoCollege"
<?php if (($_REQUEST['rb_education']) == 'NoCollege') echo 'checked'; ?>>
<font size = 4 color=cccccc>No College</font><br>
<input type=radio name="rb_education" value="College"
<?php if (($_REQUEST['rb_education']) == 'College') echo 'checked'; ?>>
<font size = 4 color=cccccc>College</font><br>
<input type=radio name="rb_education" value="Masters"
<?php if (($_REQUEST['rb_education']) == 'Masters') echo 'checked'; ?>
>
<font size = 4 color=cccccc>Masters</font><br>
<input type=radio name="rb_education" value="PhD"
<?php if (($_REQUEST['rb_education']) == 'PhD') echo 'checked'; ?>
>
<font size = 4 color=cccccc>Ph.D.</font>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
</tr>
<tr>
<td colspan = 2 align = center><input type = submit name="submit" value="submit">
</td>
</tr>
</table>
</form>
</body>
</html>

 

Form Tag Modifications.  The form tag needs to be modified like the following.

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method = "post">

In all these examples instances the PHP echo( ) function is used to turn PHP back into HTML.

Text Box Tag Modifications.  The value within the text tag is used to set the contents of the text field.  The PHP code is used to fill this in if words have been previously set.

<input type=text name="txt_last_name" size=20
value = "<?php if (isset($_REQUEST['txt_last_name'])) echo $_REQUEST['txt_last_name']; ?>">

Notice how the PHP is placed within double quotes to identify the value, but all the quotes within the PHP are single quotes.

 

Text Area Tag Modifications.  Since a textarea is delimited slightly differently than a text box the embedded PHP code is actually between the <textarea> and  </textarea> tags.

<textarea name="tarea_comments" rows = 5 columns = 20>
<?php if (isset($_REQUEST['tarea_comments'])) echo $_REQUEST['tarea_comments']; ?>
</textarea>

This PHP is not placed within double quotes, but all the quotes within the PHP are single quotes.

 

Checkbox Tag Modifications.  Whether or not a checkbox is checked is determined by the presence of the word CHECKED within the tag.  This results in something like the following..

<input type=checkbox name="chk_php" value="true"
<?php if (($_REQUEST['chk_php']) == 'true' ) echo 'checked'; ?>>
<font size = 4 color=cccccc>PHP</font>

This PHP is not placed within double quotes, but all the quotes within the PHP are single quotes.

 

Select Box Tag Modifications.  Whether or not an item in a select box is selected is determined by the presence of the word SELECTED within the tag.  This results in something like the following..

<select name="sel_credit_card">
<option value="Discover"
<?php if (($_REQUEST['sel_credit_card']) == 'Discover') echo 'selected'; ?>
>
Discover
<option value="MasterCard"
<?php if (($_REQUEST['sel_credit_card']) == 'MasterCard') echo 'selected'; ?>
>Mastercard
<option value="Visa"
<?php if (($_REQUEST['sel_credit_card']) == 'Visa') echo 'selected'; ?>
>Visa
</select>

This PHP is not placed within double quotes, but all the quotes within the PHP are single quotes.

 

Radio Button Tag Modifications.  Whether or not a radio button is checked is determined by the presence of the word CHECKED within the tag.  This results in something like the following..

<input type=radio name="rb_education" value="NoCollege"
<?php if (($_REQUEST['rb_education']) == 'NoCollege') echo 'checked'; ?>>
<font size = 4 color=cccccc>No College</font>

This PHP is not placed within double quotes, but all the quotes within the PHP are single quotes.

 

All of this will get another step more complicated when we start to interface with databases and have to be concerned about internal representations of data within MySQL and internal representations of data within HTML.  Again PHP will function as the middleware to coordinate these interactions.