Form data in PHP

See more about:

Where are we?

We just looked at the HTML tags <form>, <input>, and <button>, and saw how browsers send form data to Web servers. Let’s see how you can retrieve that data using PHP.

This lesson’s goals

By the end of this lesson, you should:

  • Know how PHP fetches form data.
  • Know how to fetch data from post and get forms.

Retrieving form data

Here’s some HTML from the previous lesson.

<h1>Simple Form</h1>
<form action="process-simple-form-post.php" method="post">
  <p>First name:
    <input name="first_name" type="text" size="20">
  </p>
  <p>Surname:
    <input name="surname" type="text" size="20">
  </p>
  <p>
    <button type="submit">Save</button>
  </p>
</form>

Figure 1. Simple form HTML

When the user clicks the button, the browser gets the data from the form, and sends it to the page given in the form’s action attribute. That’s process-simple-form-post.php.

You can try it.

Here’s process-simple-form-post.php.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title></title>
  </head>
  <body>
    <?php
    $first_name = $_POST['first_name'];
    $surname = $_POST['surname'];
    ?>
    <p>Your name is <?php print $first_name . ' ' . $surname; ?>.</p>
  </body>
</html>

Figure 2. process-simple-form-post.php

Line 9 fetches the data for one of the fields and puts it into a variable. Here is the statement again:

Fetching from $_POST

Figure 3. Fetching from $_POST

$_POST is an array holding all of the form data. You don’t need to know much about arrays yet; just follow the pattern above.

In the [''], you give the name of the HTML input field. This was specified in the HTML:

<input name="first_name" type="text" size="20">

There is a variable name in Figure 3. That’s where the form data will go.

You can name the PHP variable anything you like. Common practice is to give it the same name as the HTML form field. This makes it easier to match the HTML code with the PHP code.

Line 10 fetches the other form field’s data. It works the same way.

Line 12 in Figure 2 outputs the values. As before, print just inserts stuff into the PHP program’s output stream. Let’s look at this:

$first_name . ' ' . $surname

It’s the string concatenation operator at work again. The line tells PHP to:

Take the contents of $first_name, stick a space on the end of that, and stick the contents of $surname on the end of that.

The space (’ ‘ in the statement) is a string constant. The characters between the quotes are used exactly as-is, with no changes. For example:

$first_name . ' (Killer) ' . $surname

If $first_name was “Bugs” and $surname was “Bunny”, you would get:

Bugs (Killer) Bunny

The spaces inside the quotes are important. If we left them out:

$first_name . '(Killer)' . $surname

We’d get:

Bugs(Killer)Bunny

The spaces outside the quotes don’t matter to the PHP interpreter. We could have:

$first_name.' (Killer) '.$surname

It would still work. Some people think the extra spaces make the line more readable.

Single versus double quotes

Here’s part of line 12 again:

print $first_name . ' ' . $surname;

We could have written it like this:

print "$first_name $surname";

This version has double quotes. What’s the difference?

  • When PHP sees single quotes ('), it returns exactly what is between the quotes, with no changes.
  • When PHP sees double quotes ("), it replaces variable names with their contents, and then returns the result.

JavaScript doesn’t do this. Only PHP.

Here are some more examples.

<?php
$a = 'Willow';
$b = 'Rosenberg';
print "<p>$a $b</p>";
  //Prints <p>Willow Rosenberg</p>
print '<p>$a $b</p>';
  //Prints <p>$a $b</p>
print '<p>'. $a . ' ' . $b . '</p>';
  //Prints <p>Willow Rosenberg</p>
?>

Figure 4. Single versus double quotes

You can try it.

Post or get?

Here’s one of those statements again:

$first_name = $_POST['first_name'];

That only works for forms that use method="post":

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

If you used get:

<form action="..." method="get">

then you fetch the form data in PHP like this:

$first_name = $_GET['first_name'];

If your PHP code isn’t fetching form data, check that you are using the right method.

Exercise: Address form data

In a previous exercise, you created a form like this:

Address form

Figure 1. Address form

Add a PHP page that processes this data, producing something like:

Address output

Figure 2. Address output

You can try my solution. You can also download the files.

Upload your solution to your server. Put the URL below.

(Log in to enter your solution to this exercise.)

Summary

  • Use $_POST[‘name’] to fetch data from forms submitted with the post method.
  • Use $_GET[‘name’] to fetch data from forms submitted with the get method.

What now?

Let’s see how you deal with an annoying thing PHP does to quotes in form data.


How to...

Lessons

User login

Log in problems? Try here


Dogs