Sticky Forms

 

Introduction.  When you make use of a form on the web you may find that sometimes after submitting information and being told to go back and correct it you are sent back to an empty form!  This is extremely irritating.  If you are sent back to a form that retains the information you already entered you are said to be working with a sticky form.

Another very typical situation where sticky forms are important is when a user accesses something in a web database online using a web form.  In order to fill in the form the programmer must know how to implement sticky forms.  We will do this in more than one way when developing our user profile management interface and our administrative user profile management interface.

Unfortunately, Ullman's examples are only configured with text boxes.  In reality, it is also very important to be able to work with text areas, check boxes, radio buttons and select/option boxes.  Thus our examples, regardless of how contrived they might seem, will eventually make certain you work with all of these form controls.

Just as when working with ASPs and JSPs, PHP usually becomes the middleware that controls the interactions between HTML and whatever database management system is being used.  The tricks involved in doing this can be quite complicated as we will see in future web pages.

A First Example.  For our first example we will work a simple problem that takes a user's name, e-mail address and password.  We will also require the user to input their password a second time to increase the likelihood they know what they entered.  We will do some input validation to make certain that everything is entered and checks out.

In order to obtain information from the form you can use the naming conventions we have used so far.  You can also use one the

  • $_GET
    • used when you are using the GET to transfer information from the form to the processing script
  • $_POST
    • used when you are using the POST to transfer information from the form to the processing script
  • $_REQUEST
    • used in either situation and most reminiscent of the REQUEST class used in ASPs and JSPs.

arrays referenced by the name of the control you are looking for.

You should call this script register.php.

 

<html>
<head>
<title>A Form Page that Processes Itself</title>
</head>

<body bgcolor="003044" text="cccccc">
<?php
// making sure the form has been submitted
if (isset($_REQUEST['cmd_submit']))
{

// check for entries in the form
// check for the name

if (strlen($_REQUEST['txt_name']) > 0)
{

$txt_name =true;

}
else
{

$txt_name = false;
echo '<p>You forgot to enter your name</p>';

}
// check for the email address
if (strlen($_REQUEST['txt_email']) > 0)
{

$txt_email =true;

}
else
{

$txt_email = false;
echo '<p>You forgot to enter your e-mail address</p>';

}
// check for the password
// confirm that the first entry
// matches the confirmation entry

if (strlen($_REQUEST['txt_password1']) > 0)
{

if($_REQUEST['txt_password1'] == $_REQUEST['txt_password2'])
{

$txt_password = true;

}
else
{

echo '<p>You password entry doesn\'t match the confirmation password</p>';

}

}
else
{

$txt_password = false;
echo '<p>You forgot to enter your password</p>';

}
// taking actions based on the form entries
if ($txt_name && $txt_email && $txt_password)
{

// register the user
echo '<p>You are now registered</p>';

}
else
{

echo '<p>Given the above feedback you should<BR>go back and complete the form</p>';

}

}
else
{

?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method = "post">
<table width=500>
<tr>
<td align=center colspan=2><h2>Please enter all of the following information</h2></td>
</tr>
<tr>
<td align=right><b>Name: </b></td>
<td><input type="text" name="txt_name", size=20></td>
</tr>
<tr>
<td align=right><b>EMail Address: </b></td>
<td><input type="text" name="txt_email", size=40></td>
</tr>
<tr>
<td align=right><b>Password: </b></td>
<td><input type="password" name="txt_password1", size=20></td>
</tr>
<tr>
<td align=right><b>Confirm Password: </b></td>
<td><input type="password" name="txt_password2", size=20></td>
</tr>
<tr>
<td align=center colspan=2><input type="submit" name="cmd_submit" value = "Submit Information"></td>
</tr>
</table>
</form>

<?php
}
?>
</body>
</html>

 

Notice that the way we get the form to post to itself makes use of

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

Notice how there is PHP code embedded in the form tag.

This code makes certain at least something is entered for the name and e-mail address.  It also compares the entries for the password and makes sure they are identical.  Error messages are accumulated and actually presented on the same page without representing the form.

Another Example.  Now we will adjust the previous example to make it sticky so that the form is presented along with the messages about the user's input errors.  There isn't a lot of differences other than the form is presented regardless of the user's inputs and can be more easily modified based on the commentary above it.

You should call this file register_sticky.php.  Because the form is always presented the logic is actually a bit simipler.  But now you can focus on how the entries in the form are filled in.

 

<html>
<head>
<title>A Form Page that Processes Itself</title>
</head>

<body bgcolor="003044" text="cccccc">
<?php
// making sure the form has been submitted
if (isset($_REQUEST['cmd_submit']))
{

// check for entries in the form
// check for the name

if (strlen($_REQUEST['txt_name']) > 0)
{

$txt_name =true;

}
else
{

$txt_name = false;
echo '<br>You forgot to enter your name';

}
// check for the email address
if (strlen($_REQUEST['txt_email']) > 0)
{

$txt_email =true;

}
else
{

$txt_email = false;
echo '<br>You forgot to enter your e-mail address';

}
// check for the password
// confirm that the first entry
// matches the confirmation entry
if (strlen($_REQUEST['txt_password1']) > 0)
{

if($_REQUEST['txt_password1'] == $_REQUEST['txt_password2'])
{

$txt_password = true;

}
else
{

echo '<br>You password entry doesn\'t match the confirmation password';

}

}
else
{

$txt_password = false;
echo '<br>You forgot to enter your password';

}
// taking actions based on the form entries
if ($txt_name && $txt_email && $txt_password)
{

// register the user
echo '<br>You are now registered';

}
else
{

echo '<P>Given the above feedback you should<BR>go back and complete the form</P>';

}

}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method = "post">
<table width=500>
<tr>
<td align=center colspan=2><h2>Please enter all of the following information</h2></td>
</tr>
<tr>
<td align=right><b>Name: </b></td>
<td><input type="text" name="txt_name", size=20,
value = "<?php if (isset($_REQUEST['txt_name'])) echo $_REQUEST['txt_name']; ?>"></td>
</tr>
<tr>
<td align=right><b>EMail Address: </b></td>
<td><input type="text" name="txt_email", size=40
value = "<?php if (isset($_REQUEST['txt_email'])) echo $_REQUEST['txt_email']; ?>"></td>
</tr>
<tr>
<td align=right><b>Password: </b></td>
<td><input type="password" name="txt_password1", size=20></td>
</tr>
<tr>
<td align=right><b>Confirm Password: </b></td>
<td><input type="password" name="txt_password2", size=20></td>
</tr>
<tr>
<td align=center colspan=2><input type="submit" name="cmd_submit" value = "Submit Information"></td>
</tr>
</table>
</form>
</body>
</html>

 

I have highlighted how the value is used in the HTML tag for the text boxes to obtain the posted values using the $_REQUEST array.

value = "<?php if (isset($_REQUEST['txt_name'])) echo $_REQUEST['txt_name']; ?>"

value = "<?php if (isset($_REQUEST['txt_email'])) echo $_REQUEST['txt_email']; ?>"

These values actually contain some PHP code that displays what was entered again using the echo function.

We will work with the other form controls in the following webpage.