Lesson 6 - POST
The $_POST Superglobal
Now I will teach you about the "$_POST" Superglobal. "$_POST" is different from "$_GET" in that "$_POST" does not show the values being passed in the URL. "$_POST" Hides them in the "Headers" that are sent to the page. In every request that a browser like Firefox or IE makes, they send some information to the site that they are requesting the page from. Such as what the browser's name is, what the user's IP address is, what operating system they are using, etc... They can also "Post" additional information along with the "Headers". (Like what page of the book they are on.)
a "$_POST" URL looks like this:
http://www.booksite.com/book.php
with the page number a person is on sent hidden in the Headers. "$_POST" is a more secure form of passing variables because the average user doesn't know what is going on behind the scenes. The PHP code used to get the posted values is almost identical to the PHP code needed to get the "$_GET" value:
$book_page = $_POST['page'];
get_page($book_page);
?>
So, since we already know how posting works (you did read the last lesson right?), lets jump right in to making a script to retrieve the posted text. Now, this script will print what you enter into a box, onto the screen. Make a new text file and save it as "lesson6.php". Please use SciTE, notepad or some similar program - NOT MS Word. Now type the following into it:
if (isset($_POST['text'])) {
/* "if" something was posted (isset) do the following: */
print "You Said: ". $_POST['text'];
}
?>
If you re-save it now and upload it to your web server. When you visit the page and enter something into YOUR script's text box (and click "Submit"), you will then see it printed out on to the screen below the form. If you right-click onto the screen you will see that only the processed "source code" remains:
You Said: Hello Everybody!
Now what does each part of this code do? The first part is just a simple HTML Form that makes a input and submit box:
There is NO PHP in it - all it does is make the text area and the submit button for the script. Notice the start of the form says "
0 Responses