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" .../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" |
|
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 php5eval.php --> The initial process page 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! if($_GET['Age']>25 || $_GET['License'] == "y"
|| ($_GET['License'] == "n" && $_GET['Car'] == "y")) NOTE: You are actually now PROGRAMMING a programming language. If a form is not needed, the form tag should NOT appear. The specifics Now read this REAL carefully echo "<INPUT TYPE=\"HIDDEN\" NAME='Age' VALUE='{$_GET['Age']}'>";
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: php5final.php --> The final process page if(!isset($_POST['numYearsWithLicense']))
// means this variable has NOT been posted (! means not or opposite) 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 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. |