PHP - Capturing Form Data with PHP


First of all, the form's action attribute needs to contain the URL of the PHP script that will handle the form.

For example:

<form action="form_handler.php"method="post">

When users send their forms, their data is sent to the server and the form_handler.php script is run.

The script then needs to read the form data and act on it.

Superglobal Array Description
$_GET Contains a list of all the field names and values sent by a form using the get method
$_POST Contains a list of all the field names and values sent by a form using the post method
$_REQUEST Contains the values of both the $_GET and $_POST arrays combined, along with the values of the $_COOKIE superglobal array

Each of these three superglobal arrays contains the field names from the sent form as array keys, with the field values themselves as array values.

For example, suppose you created a form using the get method, and that form contained the following control:

<input type="text" name="emailAddress"value=""/ >

You could then access the value that the user entered into that form field using either the $_GET or the $_REQUEST superglobal:

$email = $_GET["emailAddress"];
$email = $_REQUEST["emailAddress"];


In this example, you create a simple user registration form, then write a form handler script that reads the field values sent from the form and displays them in the page.

First, create the registration form.

Save the following HTML code as registration.html in your document root folder:

    <title>Membership Form</title>
    Membership Form
    <p>To register, please fill in your details below and click Send Details.</p>

    <form action="process_registration.php"method="post">
      <div style="width: 30em;">
        <label for="firstName">First name</label>
        <input type="text"name="firstName"id="firstName"value=""/>

        <label for="lastName">Last name</label>
        <input type="text"name="lastName"id="lastName"value=""/>

        <label for="password1">Choose a password</label>
        <input type="password"name="password1"id="password1"value=""/>
        <label for="password2">Retype password</label>
        <input type="password"name="password2"id="password2"value=""/>

        <label for="genderMale">Are you male...</label>
        <input type="radio"name="gender"id="genderMale"value="M"/>
        <label for="genderFemale">...or female?</label>
        <input type="radio"name="gender"id="genderFemale"value="F"/>

        <label for="favoriteWidget">What's your favorite widget?</label>
        <select name="favoriteWidget"id="favoriteWidget"size="1">
          <option value="superWidget">The SuperWidget</option>
          <option value="megaWidget">The MegaWidget</option>
          <option value="wonderWidget">The WonderWidget</option>

        <label for="newsletter">Do you want to receive our newsletter?</label>
        <input type="checkbox"name="newsletter"id="newsletter"value="yes"/>

        <label for="comments">Any comments?</label>
        <textarea name="comments"id="comments"rows="4"cols="50"> </textarea>
        <div style="clear: both;">
          <input type="submit"name="submitButton"id="submitButton"value="Send Details"/>
          <input type="reset"name="resetButton"id="resetButton"value="Reset Form"style="margin-right: 20px;"/>


Next, save the following script as process_registration.php in your document root, the folder where you placed registration.html.

    <title>Thank You</title>
    Thank You

    <p>Thank you for registering. Here is the information you submitted:</p>

      <dt>First name</dt><dd><?php echo $_POST["firstName"]?></dd>
      <dt>Last name</dt><dd><?php echo $_POST["lastName"]?></dd>
      <dt>Password</dt><dd><?php echo $_POST["password1"]?></dd>
      <dt>Retyped password</dt><dd><?php echo $_POST["password2"]?></dd>
      <dt>Gender</dt><dd><?php echo $_POST["gender"]?></dd>
      <dt>Favorite widget</dt><dd><?php echo $_POST["favoriteWidget"]?></dd>
      <dt>Do you want to receive our newsletter?</dt><dd><?php echo $_POST["newsletter"]?></dd>
      <dt>Comments</dt><dd><?php echo $_POST["comments"]?></dd>

Fill in the fields in the form, then click the Send Details button.

If all goes well, you should see a page displaying the data that you just entered.

Related Topic