Star Hoster
Photoshop Tutorials
Anonymous Web Surfing
Funny Jokes
Myspace Friends
Open Treasure Chest
Building in Paradise


July 1, 2006

PHP Post And Get Form Method

Filed under: Intro PHP Tutorials — phpdeveloper @ 1:08 am

Post and get method of form.

Recall from the PHP Forms Lesson where we used an HTML form and sent it to a PHP web page for processing. In that lesson we opted to use the the post method for submitting, but we could have also chosen the get method. This lesson will review both transferring methods.

POST

In our PHP Forms Lesson we used the post method. This is what the pertinent line of HTML code looked like:

HTML Code Excerpt:

This HTML code specifies that the form data will be submitted to the “newform.php” web page using the POST method. The way that PHP does this is to store all the “posted” values into an associative array called “$_POST”. Be sure to take notice the names of the form data names, as they represent the keys in the “$_POST” associative array.

Now that you know about associative arrays, the PHP code from “newform.php” should make a litte more sense.

PHP Code :

$quantity = $_POST[’quantity’];
$prod = $_POST[’product’];
The form names are used as the keys in the associative array, so be sure that you never have two input items in your HTML form that have the same name. If you do, then you might see some problems arise.

GET

As we mentioned before, the alternative to the post method is get. If we were to change our HTML form to the get method, it would look like this:
HTML Code

The get method is different in that it passes the variables along to the “newform.php” web page by appending them onto the end of the URL. The URL, after clicking submit, would have this added on to the end of it:

“?product=##&quantity=##”

The question mark “?” tells the browser that the following items are variables. Now that we changed the method of sending information on “order.html”, we must change the “newform.php” code to use the “$_GET” associative array.

PHP Code :

$quantity = $_GET[’quantity’];
$prod = $_GET[’product’];

After changing the array name the script will function properly. Using the get method displays the variable information to your visitor, so be sure you are not sending password information or other sensitive items with the get method.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

You must be logged in to post a comment.