Day Five: HTML Forms and PHP

PHP was designed to specifically to create and process HTML documents. As a result, it works well with HTML forms. The data from the form simply shows up in your PHP document as a named variable that you can manipulate like any other PHP variable.

Before we start using forms, I would like to review the terminology used in creating forms. There are three ways to pass data into a PHP program. These mechanisms are called GET and POST and cookies. 

Get It?

The GET mechanism works by adding information to the URL of your page. You will use this mechanism when you create links. Lets say your URL was myPage.html; You can add GET information by appending a question mark, and name value pairs in the data. The get mechanism uses the question mark as a delimiter between the URL and your variables. It uses the ampersand (&) as a delimiter between your named value pairs.

For example, if your PHP document was named test.html, you could pass GET information by appending ?varOne=123&varTwo=456. (See Sample 1) 

Sample One
Step One: Write the following page in PHP and save it as sample5_1.html.
  echo "Start of Page";
  echo "<br>Variable One: $varOne";
  echo "<br>Variable Two: $varTwo";

Now open a web browser, and call you page with the URL "sample5_1.html?varOne=123&varTwo=456"

Your page should show the value 123 in $varOne, and 456 in $varTwo. You can change the values of the variables, and see the results.

It is easy to use the GET mechanism. Unfortunately, the GET mechanism is subject to all of the vague rules used to create URLs. For example, you cannot use spaces, commas, question marks or several other characters in the URL string, and older browsers limit the length of the URL string. You have to replace the space character with a plus sign +. You replace the comma with the code %2C, etc..

PHP includes programs for encoding and decoding URLs. These have the urlencode and urldecode. Since you have your browser open you might want to change the code in test.html to the following:

Sample Two
Step One: Write the following page in PHP and save it as sample5_2.html.

$str="This string, is long, has spaces, a +, commas & an ampersand.";

echo "<b>The original string is:</b> ".$str;
echo "<br><b>The encoded string is:</b> ".$encoded;

echo "<br><b>Test Link::</b> <a href=sample5_2.html?varOne=$encoded>Sample5.2</a>";

if ($varOne=="") {
  echo "<br><b>Press the test link to run the test!</b>";
} else {
  echo "<br><b>The string passed to PHP is:</b> ".$varOne;


Open the page in your browser, and press the test link. The url should

URL encoding is a hassle. To make my life easier. I usually only use the get mechanism for links that do not use spaces. For example, I use the get to pass a numeric primary key, or a special spaceless code that triggers an action.


You can also pass information to PHP in the form of a cookie. Since I intend to spend most of the class time on forms, I thought I would touch cookies with a brief technical discussion.

A cookie is a small file that a web browser puts on a client's computer. When a person accesses your web site, their browser will read the cookie file, then pass that information to your web site in the HTTP header. If you look through the Temporary Internet files on your computer, you will find several hundred cookie files from different web sites.

The cookie is part of the HTTP header. The HTTP header is a formatted record that a client's browser sends to server every time they access the page. The HTTP header tells the server where to send the page. It contains information such as the client's IP address, their host domain, the referring page, and any information you add to the header.

It is kind of confusing. Web pages have multiple headers. The HTTP Header is a hidden record that communications information between the client's computer and the server computer. Web pages have an HTML Header that contains the title of the page and other information. Finally, you can put headers in your document. There are six levels of headers numbered from <h1> to <h6>. You use these headers for creating a document outline.

Since the cookie is part of the HTTP Header, you must output the cookie before sending any text information to the document. You cookie code must come before the first echo statement.

You set a cookie with the setcookie command. The command setcookie("myCookie",1234) will create a cookie called myCookie. PHP will translate this cookie into a variable called $myCookie. Here is our test cookie sample.


Sample Three - Test Cookie
Step One: Write the following page in PHP and save it as sample5_3.html.
// The php code has to start at the very top of the page.
// Since I cannot echo any text until after I set the cookie,
// I will buffer text in the variable $html, then echo it to
// the screen after I set the cookie.
if ($myCookie=="") {
  // The cookie is not set, ask the user to set the cookie.
  $html="<p>The cookie is not set. Refresh the screen to set the cookie.</p>";
  // I set the cookie with the value 1.
} else {
  $html="The value of the cookie is $myCookie. Let's try adding one to the cookie.
Press refresh.";
// now that I have set the cookie, I can begin echoing HTML.
// I will print the HTML header, and put the value of $html into my header.
echo "<html><head><title>Sample 5.3 -- Test Cookie</title></head>";
echo "<table align=center><tr><td>".$html."</td></tr></table>";


Open the page in your browser, and press the test link. The url should

Cookies can get frustrating. First, not all browsers use cookies. Second, you have to send the cookie before sending any other text. Different browsers handle cookies differently. This is especially true with persistent cookies, which I will not cover here.

HTML Forms.

Finally we get to the meat of today's class. HTML forms.

index -- next
math, plusroot, search, rgreetings, sponsors