Home » PHP, Tech Corner

PHP Forms

4 August 2009 No Comments Posted By:Vivek

As you would have noticed in the earlier posts,the $_GET and $_POST variables are used to retrieve information from a web form.All HTML form elements are automatically available to the PHP script.Lets see how the form elements are made available to the PHP script.

<html>
<body>

<form action=”welcome.php” method=”post”>
Name: <input type=”text” name=”username” />
<input type=”submit” />
</form>

</body>
</html>

Now when the submit button is clicked,the “welcome.php” script is called,which looks something like this

<html>
<body>

Welcome <?php echo $_POST["username"]; ?>!

</body>
</html>

The above form passes values using the “post” method and hence the $_POST function is used to retrieve the form data.

The $_GET environment variable is used to retrieve data from forms with the “get” method.The main difference between the $_GET and $_POST is that the information that is passed from the form is visible to everyone,(it will be visible in the browser) and also there is a space limitation on the amount of information that can be send.

Lets see the above example using the $_GET function

<html>
<body>

<form action=”welcome.php” method=”get”>
Name: <input type=”text” name=”username” />
<input type=”submit” />
</form>

</body>
</html>

Here to retrieve the value,we use the $_GET function in the welcome.php script

<html>
<body>

Welcome <?php echo $_GET["username"]; ?>!

</body>
</html>


The difference between the $_GET and $_POST functions can be noticed in the URLs of the two.

URL using get

URL using get

URL using post

URL using post

As such,we should not use the $_GET function to pass sensitive data like password or credit card numbers.Also the amount of information that can be passed should not exceed 1024 characters.The variables are displayed in the URL and hence $_GET allows bookmarking of the web pages.Also,we cannot send binary data like images to the web server using get method.

When using $_POST,there is no limit in the amount of information that can be passed and also the data is invisible.But,since the variables are not available with the URL,we cannot bookmark the web page.

The $_REQUEST function can be used to collect information sent with both get or post methods.

<html>
<body>

Welcome <?php echo $_REQUEST["username"]; ?>!

</body>
</html>

Related posts:

  1. Data Encryption in PHP using crypt()
  2. Cookies and Sessions in PHP
  3. PHP and MySQL Basics
  4. Building a Login Form using PHP/MySQL
  5. Creating user defined functions and classes in PHP

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>