Jump to content

  •  

  • iBotModz CBox


    Photo

    Beginning PHP - Lesson 6 - Simple Script


    This topic has been archived. This means that you cannot reply to this topic.
    No replies to this topic

    #1 fattwam

    fattwam

      General Grade 2

    • Sub-Admins

    • 3,979 posts
    • Joined: 18-August 07

    Posted 27 March 2008 - 08:13 PM

    Simple $_GET and $_POST script.


    Now it is time to put what we learned about $_GET and $_POST to use. We are going to make a script that prints what you enter into a box onto the screen.

    Make a new text file and save it as "post.php" with (use 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 you will see the something like the following:

    <form>
    Your Text:<input name="text" type="text">
    <input type="submit" value="Submit">
    </form>

    When you enter something into YOUR scripts 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:

    <form action="post.php" method="post">		  <!--//Make the HTML Form -->
    Your Text:<input name="text" type="text">	   <!--//Make the input box -->
    <input type="submit" value="Submit">		<!--//Make the Submit button -->
    </form>							 <!--// End the Form -->
    You Said: Hello Everybody!

    Now what does each part of this code do? The first part is just a simple HTML Form:

    <form action="post.php" method="post">		  <!--//Make the HTML Form -->
    Your Text:<input name="text" type="text">	   <!--//Make the input box -->
    <input type="submit" value="Submit">		<!--//Make the Submit button -->
    </form>										 <!--// End the Form -->

    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 "post.php". In other words, whatever is entered into the form should be sent to the post.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.


    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, and 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";
    ?>






    About "if":

    "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). Statements usually end with a semicolon. 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)
    then do statement




    For more information please read these articles:

    Using If Else Statements
    Operators
    Looping The Loop

    If you find a broken link please PM me, thanks!