$_POST

  • Description : In this tutorial you will learn how to use the POST function.
  • Language : PHP
  • Date Added : 14th October, 2009
Bookmark and Share

Let's start by looking at that form again from the previous tutorial on using GET, except there is one small change in the method parameter:

Code :
<form action="" method="post"> Pet Name: <input type="text" name="petname" /><br /> Pet Age: <input type="text" name="petage" /><br /> <input type="submit" name="submit" value="submit" /> </form>
Output:
Pet Name:
Pet Age:

Although there is only a minor change to the code the URL looks very different:

Output:
http://www.yourdomainhere.com/postform.php

There are no visible parameters and the page keeps the data secure and not publicly visible. Below is the code for retrieving the data and display it, you'll notice it's very similar to the GET function:

Code :
<?php $petName = $_POST['petname']; $petAge = $_POST['petage']; echo "Your pet's name is " . $petName . "<br />"; echo "Your pet's age is " . $petAge; ?>

If you entered "dennis" and "10" the output would be:

Output:
Your pet's name is dennis Your pet's age is 10

So when should the POST function be used? Mainly when handling data that is private, such as passwords. It shouldn't be used when wanting to allow users to bookmark a page as it will not work correctly. You can see a working example of the form by clicking here. In the next tutorial we will look at validating data submitted from a form!



Login Required

To post comments you must sign in to your account below!

User :
Pass :

If you do not currently have an account click here to create one now!