1.1. Using Static HTML Forms

HTML forms are very important for interactive Web sites. In your previous experience, you might have displayed static HTML forms on Web pages. That is, forms whose content is predetermined and cannot change. In this section, you see how to display a static HTML form from a PHP script.

1.1.1. Displaying an HTML form

To display a form with PHP, you can do one of the following:

  • Use echo statements to echo the HTML for a form. For example:

    <?php
      echo "<form action='process_form.php'
                  method='POST'>\n
         <input type='text' name='fullname' />\n
         <input type='submit' value='Submit Name' />\n
         </form>\n";
    ?>
  • Use plain HTML outside the PHP sections. For a plain static form, there's no reason to include it in a PHP section. For example:

    <?php
       statements in PHP section
    ?>
    <form action="process_form.php" method="POST">
      <input type="text" name="fullname" />
      <input type="submit" value="Submit Name" />
    </form>
    <?php
      statements in PHP section
    ?>

Either of these methods produces the form displayed in Figure 1-1.

Figure 1-1. A form produced by HTML statements.

1.1.2. Getting information from the form

Joe Customer fills in the HTML form. He clicks the Submit Name button. You now have the information that you wanted — his name. So where is it? How do you get it?

You get the form information by running a script that receives the form information. When the user clicks the submit ...

Get PHP & MySQL® Web Development All-in-One Desk Reference for Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.