Categories:

    $_POST Lesson 6

    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:


    Your Text:



    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:


    Your Text:


    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:


    Your Text:

    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 "

    ". This is telling your browser to "POST" the value in the form to the web page "lesson6.php". In other words, whatever is entered into the form should be sent to the lesson6.php page by way of the "$_POST" superglobal.

    The next six lines are the PHP code that checks to see if anything was posted:

    if (isset($_POST['text'])) {
    /* "if" something was posted (isset($_POST['text'])) do the following: */
    print "You Said: ". $_POST['text'];
    }
    ?>

    If something is in the "$_POST" superglobal it will "print" it out. In English the code would read:

    if (something isset( inside of $_POST['text'])) { then we need to:
    print
    the following to the screen: "You Said: " plus (.) the value of $_POST['text'];
    }
    ?>

    About "if"

    The most important part of this script was the "if". "If" checks to see if the expression is TRUE and if it is - the "if" will run the code below it. In other words - If an expression evaluates to TRUE, PHP will execute the following statement, but if it evaluates to FALSE - it'll ignore it.

    The following example would display "a is bigger than b" if the variable $a is bigger than $b:

    if ($a > $b) //Greater than ">"
    print "a is bigger than b";
    ?>
    Quote:
    Any PHP script is built out of a series of statements. A statement can be an assignment, a function call, a loop, a conditional statement of even a statement that does nothing (an empty statement). In addition, statements can be grouped into a statement-group by encapsulating a group of statements with curly braces. A statement-group is a statement by itself as well.

    The if construct is one of the most important features of many languages, PHP included. It allows for conditional execution of code fragments. PHP features an if structure that is similar to that of C: -PHP.net

    if (expression = TRUE)
    then do statement
    ?>

    For more information on the "if" statement please read these articles:

    0 Responses