Previous --> Page 4 |This is Page 5 | Next --> Page 6

A deeper understanding of forms

A form tag typically has the following syntax:

<FORM NAME="form1" METHOD="GET/POST" ACTION="processPage.php">

Note that a form must use one of two methods, GET or POST.

METHOD="GET"
This method appends the values for the form elements to the processing page's URL as follows:
If you have a page with a form with three INPUTs (let's call them Name, Age, FavColor), if you are using the GET method, the page that is called in the ACTION part of the form will look as follows:

.../processPage.php?Name=John&Age=45&FavColor=blue

Notice the syntax here - a "?" is put after the webpage name. Next comes each form element (an input box, select, checkbox, etc.), an equal sign, and the value typed in by the user. Further notice that each form element is separated by "&"

These are called URL parameters (or variables). They can be accessed this way: $_GET['ParamName'], ie. $_GET['Name']

METHOD="POST"
This method 'posts' the values for the form elements to the server, invisible to the website viewer.
The only way to access them (see their values) is: $_POST['ParamName'], ie. $_POST['Name']

So what's the big deal? When should I use one over another?

SECURITY, SECURITY, SECURITY! That is the biggest one. Imagine you had a form asking for a username and password. If you used the GET method, whatever a user typed would become part of the URL. Someone else might use the computer later and be able to look at the history page (or click that dropdown box that shows previous pages viewed) and find this information. With that said, when the information should not be public, use POST. If the information is harmless, GET works fine.

GET variables can come in handy if you want a quick way to look for data. Imagine you have a blog and you can see a day's entry by specifying the month, day, and year. If the information comes in through URL parameters, rather than having to hit submit on a form (and taking the time to fill it out), you can just directly edit the URL parameters and hit enter.

One last word of caution: if your form has A LOT of elements on it (many inputs/questions, etc.), using GET could result in a VERY LONG URL (because all of the form elements have to be appended to it.) POST is used slightly more often than GET, but the choice is yours.

CHECKING TO SEE IF A VARIABLE 'EXISTS' (Has been POSTED or is a URL parameter.)

Imagine you have a website where people login. Depending upon their access privileges, a form they fill out may ask different questions. You can ask if a variable has been POSTED or is part of the URL with the following syntax:

if(isset($_POST['myVoteIn2000'])) // isset() checks to see if that variable is "set"
{ // if the variable does exist, isset returns true (otherwise false)
  $votedFor = $_POST['myVoteIn2000'];
  if($votedFor == "Bush")
    echo "The person you voted for won.";
  else // probably Gore but possibly someone else
    echo "The person you voted for lost.";
}
else
{
  echo "You were not old enough to vote in 2000.";
}

You were all barely alive in the year 2000, let alone of voting age, so if the database knew your age, it wouldn't even ask you on the form who you voted for. Therefore that form element would not appear, meaning it would not be "set" (or sent to the process page).

This is an important concept because a variable may exist but have an "empty" value, such as $nothing = ""; The variable $nothing exists but its value is the empty string.

ASSIGNMENT 5 - Making your program smart

This assignment is a bit tedious, so please read it thoroughly and refer back to each section as you complete it.

php5.php --> The initial form
Create a page with a form that asks for the following: Age (input box), "Have a license?", "Have a car?" The last two elements should be radio buttons (Yes/No, Yes/No) The action should be php5eval.php and the method should be GET.

php5eval.php --> The initial process page
This page will possibly generate ANOTHER FORM based upon the information provided. Read this carefully!

If the person is 25 or older, has a license, or does not have a license but has a car, there will be a form!
To do this, you will need an IF statement that looks something like

if($_GET['Age']>25 || $_GET['License'] == "y" || ($_GET['License'] == "n" && $_GET['Car'] == "y"))
{ echo "<FORM...>";}

NOTE: You are actually now PROGRAMMING a programming language. If a form is not needed, the form tag should NOT appear.

The specifics
If the age is 25 or greater, have a form element that asks if the person has ever rented a car. (If the age is less than 25, do NOT ask.)
If the person has a license, ask the person how long he/she has had a license for.
If the person does NOT have a license, simply PRINT TO THE SCREEN, "You're better off without a license."
If the person does NOT have a license but DOES have a car, have a form element (input box) that asks, "Why do you have a car?"

Now read this REAL carefully
If this page has a single form element, ensure that the form has a submit button. If there is no need for a form, do NOT have a submit button. If a form is needed, its action should be php5final.php with a method of POST. In addition, if a form is needed, we want to perpetuate (pass through) the URL variable Age that was entered on the initial form. To do this, include a HIDDEN FIELD on the form (on php5eval.php) like so:

echo "<INPUT TYPE=\"HIDDEN\" NAME='Age' VALUE='{$_GET['Age']}'>";

A FEW NEW THINGS TO NOTE ABOUT ECHO

1. Echo can be used without quotation marks in some select instances (although you ALWAYS can use them.)
2. When using quotation marks, it is easiest to use single quotes inside of them for code that requires it.
3. If you prefer to use a quotation mark within the echo, you must PRECEDE IT with a backslash which tells the computer you want a quotation mark to appear, NOT to end the echo (which then causes an error.)
4. POST and GET variables are actually part of an "associative array" (rather than a numeric slot array starting at 0, each slot has a name). To echo a value from an associative array when using quotation marks for the echo, the array slot must be contained within braces.

If a form is NOT needed, we still want to call the process page that follows. We can accomplish this by making a link that sends the age as a URL parameter:
echo "<A HREF='php5final.php?Age={$_GET['Age']}'>Click to continue</A>";
Note that by doing this, no other variables will be SET (of the POST or GET variety) .

php5final.php --> The final process page
This is where the fun is! The first thing this page should do is print out "You are # years old." (# is the actual age)
--> Remember, the Age will either be coming in as a POST or GET depending upon the two previous pages.
The next thing it should do is print either "You don't have a license" or "You have had a license for # years."
--> Note that the previous page has a form element asking how many years he/she has had a license IF he/she has a license. If the person does not have a license, then no variable (ie numYearsWithLicense) will be posted to this page. Therefore, you could ask:

if(!isset($_POST['numYearsWithLicense'])) // means this variable has NOT been posted (! means not or opposite)
{ echo "You don't have a license."; } else { /* print # years with license */ }

Lastly, if the variable is POSTed for not having a license but having a car (the reason why), print "Why I have a car: <<the reason>>" (printing what they typed as the reason.)

SUMMARY
This assignment has 3 pages. You will need to use IF statements to make decisions about what should appear on the screen. You will be asking if variables are set using isset() and also referencing variables as either $_POST[] or $_GET[]. You will be literally scripting the HTML within PHP echo tags. If you need a form, you will write the HTML code within an echo.

You may want to create other variables to stay organized - that is OK. You may want to write $age = $_POST['Age']; to avoid typing so much later on. Just remember, that variable will only exist on that one page.

This assignment is primarily testing your logic skills and ability to follow instructions. I have given you helpful code and feel free to use any online resource to complete this assignment.