Code2Design.com

User login

Programming

The Layout

Navigation

Popular content

Resources

Who's online

There are currently 2 users and 10 guests online.

Online users

  • DyemAquamma
  • Maryonne

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:

<?php
$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:

<form action="lesson6.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 -->

<?php
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:

<form action="lesson6.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 that makes a input and submit box:

<form action="lesson6.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 "<form action="lesson6.php" method="post">". 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:

<?php
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:

<?php
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:

<?php
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

<?php
if (expression TRUE)
   
then do statement
?>

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

Using If Else Statements
Operators
Looping The Loop

If you find a broken link or want to add an article to the list please PM me, thanks!

Watch the Movie of Lesson 6


Submitted by David on November 9, 2006 - 6:04pm.
printer friendly version

In this lesson, even if I

In this lesson, even if I left the field blank, it still output "You Said:"...shouldn't the if statement have stopped anything from being output??


Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <br /> <h3>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use BBCode tags in the text, URLs will be automatically converted to links
More information about formatting options



Like what you see?

Why not add more? C2D is looking for other Christian Web Masters who would like to help write articles for this site. If you have expericance in FLASH, CSS/HTML, PHP/MySQL, PhotoShop/GIMP, Blender, Javascript, or just General Design - our users would love to hear what you have to say. Contact Us

delicious   digg   reddit   magnoliacom   newsvine   furl   google   yahoo   technorati