Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > PHP/MySQL/Scripting

  • Print
  • + Share This
This chapter is from the book

Making Sticky Forms

You've certainly come across sticky forms, even if you didn't know that's what they were called. A sticky form is simply a standard HTML form that remembers how you filled it out. This is a particularly nice feature for end users, especially if you are requiring them to resubmit a form.

To preset what's entered in a text box, use its value attribute:

<input type="text" name="name" size="20" maxlength="40" value="Brian" />

To have PHP preset that value, print the appropriate variable:

<input type="text" name="name" size="20" maxlength="40" value="<?php echo $_POST['name']; 
?>" />

With this in mind, I'll rewrite register.php one final time so that it's stickier.

To make a sticky form:

  1. Open register.php (refer to Script 3.13) in your text editor.
  2. Change the Name input to read (Script 3.15)
    <p><b>Name:</b> <input type="text" name="name" size="20" maxlength="40" value="<?php if (
    isset($_POST['name'])) echo $_POST['name']; ?>" /></p>
    
    The first thing I've done here is to add the value attribute to the input. Then, I print out the value of the submitted name variable ($_POST['name']). But first I want to make sure it has a value, so I check that the variable is set. The end result for the input's value is the PHP code
    <?php
    if (isset($_POST['name'])) {
       echo $_POST['name'];
    }
    ?>
    
    which I've condensed to its most minimal form.
  3. Repeat the process for the Email Address and User Name.
    <p><b>Email Address:</b> <input type="text" name="email" size="40" maxlength="60" 
    value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" /></p>
    <p><b>User Name:</b> <input type="text" name="username" size="20" maxlength="40" 
    value="<?php if (isset($_POST['username'])) echo $_POST['username']; ?>" /></p>
    
  4. Save the file as register.php, upload to your Web server, and test in your Web browser ( Figure 3.23 ).
    03fig23.gif

    Figure 3.23 This more professional version of the form remembers most of what the user previously entered.

Example 3.15. The registration form now remembers values the user submitted.

1   <?php # Script 3.15 - register.php
2
3   if (isset($_POST['submit'])) { // Handle the form.
4
5      $message = NULL; // Create an empty new variable.
6
7      // Check for a name.
8      if (strlen($_POST['name']) > 0) {
9          $name = TRUE;
10     } else {
11         $name = FALSE;
12         $message .= '<p>You forgot to enter your name!</p>';
13     }
14
15     // Check for an email address.
16     if (strlen($_POST['email']) > 0) {
17         $email = TRUE;
18     } else {
19         $email = FALSE;
20         $message .= '<p>You forgot to enter your email address!</p>';
21     }
22
23     // Check for a username.
24     if (strlen($_POST['username']) > 0) {
25         $username = TRUE;
26     } else {
27         $username = FALSE;
28         $message .= '<p>You forgot to enter your user name!</p>';
29     }
30
31     // Check for a password and match against the confirmed password.
32     if (strlen($_POST['password1']) > 0) {
33         if ($_POST['password1'] == $_POST['password2']) {
34             $password = TRUE;
35         } else {
36             $password = FALSE;
37             $message .= '<p>Your password did not match the confirmed password!</p>';
38         }
39     } else {
40         $password = FALSE;
41         $message .= '<p>You forgot to enter your password!</p>';
42     }
43
44     if ($name && $email && $username && $password) { // If everything's okayOK.
45         // Register the user.
46
47         // Send an email.
48         $body = "Thank you for registering with our site!\nYour username is
 '{$_POST['username']}' and your password is '{$_POST['password1']}'.\n\nSincerely,\nUs";
49         mail ($_POST['email'], 'Thank you for registering!', $body, 'From: admin@site
.com');
50

51         header ('Location: thankyou.php');
52         exit();
53     } else {
54         $message .= '<p>Please try again.</p>';
55     }
56
57  }
58
59  // Set the page title and include the HTML header.
60  $page_title = 'Register!';
61  include ('./header.inc');
62
63  // Print the error message if there is one.
64  if (isset($message)) {
65      echo '<font color="red">', $message, '</font>';
66  }
67  ?>
68
69  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
70  <fieldset><legend>Enter your information in the form below:</legend>
71
72  <p><b>Name:</b> <input type="text" name="name" size="20" maxlength="40" value="<?php 
if (isset($_POST['name'])) echo $_POST['name']; ?>" /></p>
73
74  <p><b>Email Address:</b> <input type="text" name="email" size="40" maxlength="60" 
value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" /></p>
75
76  <p><b>User Name:</b> <input type="text" name="username" size="20" maxlength="40" 
value="<?php if (isset($_POST['username'])) echo $_POST['username']; ?>" /></p>
77
78  <p><b>Password:</b> <input type="password" name="password1" size="20" maxlength="40" /></p>
79
80  <p><b>Confirm Password:</b> <input type="password" name="password2" size="20" 
maxlength="40" /></p>
81  </fieldset>
82
83  <div align="center"><input type="submit" name="submit" value="Submit Information" /></div>
84
85  </form><!-- End of Form -->
86
87  <?php
88  include ('./footer.inc'); // Include the HTML footer.
89  ?>h
  • + Share This
  • 🔖 Save To Your Account