PHP Notes - Intelligent Forms

What is an Intelligent Form?

Have you ever used a form that rejected your submission because it wasn't filled in completely, but then returned you to the original form with most of the information that you filled out still intact? That is an intelligent form.

The alternative, of course, is a form that returns you to "ground zero", and you must enter everything over again. Most people would prefer an intelligent form, and here is how that can be done with PHP.


One Way to Make an Intelligent Form

The basic structure of the intelligent form used in this example is as follows:
Header

Optional things printed an all versions.
Define Function to Print Form

Form Variables should be initialized with
    default or null fields for first call.

Decision Area

If first call
    Call function with no arguments
    (Print Default Form)

Else
        If partially filled out or invalid
            Complain and call function with valid arguments
            (Reprint form with some fields entered)

        Else
           
Process completed form

Footer

Optional things printed on all versions.




An Example Intelligent Form

Consider the following example of a Suggestion Box program where people can submit comments about their classes.

suggestion.php3
The Header
This section is rather simple. It just sets up a heading and an introduction.
<HTML>
<BODY BGCOLOR = "FFFFFF">
<CENTER>
<H1> <FONT COLOR="#FF0000">Suggestion Box</FONT></H1>
</CENTER>
This is for entering a suggestion about your class in the CS Lab.
The Function Definition
The following function written in PHP expects three parameters, the person's name, the period,and the comment. The $name and $comment variables are set to null, whereas $period is initialized to 8. There is an array called $options[] which is used to generate the OPTIONS list under the SELECT tag. The OPTION SELECTED is created by matching one of the periods in the array. The TEXTAREA is for the comment.

If the function is called without arguments, the default values will be assigned to the variables. Otherwise, after the SUBMIT button is pressed, new values will be assigned to the variables depending upon what the user enters. Since the ACTION of the form calls itself, different display options are possible depending upon form values, as will be seen in the Decision Section below.

<?
function show_form($name = "", $period="8", $comment="" )
 {
  $options = array("1", "2", "3", "4", "5", "6", "7", "8");
  echo "<FORM ACTION = 'suggestion.php3' METHOD = 'POST'>";

  echo "Enter Name:";
  echo "<INPUT TYPE=TEXT  NAME=name  VALUE = '$name'>";
  echo "<BR><BR><B>Select Period:</B>";
  echo "<SELECT NAME = period>";

  for ($i=0; $i<count($options); $i++) {
    echo "<OPTION";
    if($period == $options[$i]){
      echo " SELECTED ";
    }
    echo "> $options[$i]\n";
  }
  echo "</SELECT> <BR><BR>";
  echo "<TEXTAREA NAME=comment COLS=40 ROWS=3>";
  echo "$comment</TEXTAREA><BR><BR>";
  echo "<INPUT TYPE = SUBMIT VALUE = 'Submit Suggestion'> ";

 }
?>
The Decision Section
In this section, we select among three options: 1) the first time the page is called, 2) when the page is called with wrong values or items that have not been entered, and 3) when the page entry is complete and the form can be processed by saving the suggestion to a file.

The first time this page is called, the variable $name is not set. Thus, the function is called with no parameters which sets the default values. After a user presses the SUBMIT button, the else clause will be entered. If any of the input values are unacceptable, an error message is printed and the form is redisplayed with known parameters this time.

If all the input variables are satisfactory, the Decision Section falls into the last else clause. In that section, a message is printed and the data is appended to an existing file, "suggestionbox.txt". A function called stripslashes() is called in order to remove any escape sequences put in fron of characters such as quotes or apostrophes. It is also important to note that the text file must be world writable (chmod 666) in order to allow file writes from the web on our systems.

<?
if (!isset($name) ){
  show_form();
  }
else {
    if(empty($name) || $period==8 || empty($comment) ) {
       echo "You did not fill in all the fields. <BR>";
       echo "<B>Please try again.</B>";
       if ($period == 8) echo "<BR>There is no class here 8th Period!";
       show_form( $name, $period, $comment);
       }
    else {
       echo " Thank you $name in period $period.  Your suggestion is:<BR>";
       echo " <TABLE BORDER=1 WIDTH=50%><TR><TD BGCOLOR='#DDEEFF'>";
       $comment = stripslashes($comment);
       echo " $comment";
       echo " </TD></TR></TABLE>";
       $fp = fopen("suggestionbox.txt", "a");
       fputs ($fp, "Name:".$name."\n");
       fputs ($fp, "Period:".$period."\n");
       fputs ($fp, $comment."\n");
       fclose($fp);

      }
   }
?>      
  
The Footer
There is not too much exciting stuff going on here, but that is typical.
</BODY>                                                       
</HTML>   



The Suggestion Box Program