Submitting form data with PHP (GET and POST)

There are two ways to submit data in PHP.

  • The GET Method
  • The POST Method

The browser sends the information and encodes it using URL encoding. After the information is encoded it is sent to the server.

The GET Method

The GET method sends the encoded information to the page request. The page and the encoded information are separated by the ? character.

http://www.abc.com/index.htm?name1=value1&name2=value2

The GET method produces a long string that appears in the server logs, in the browser’s Location: box.
The GET method is restricted to send upto 1024 characters only.

It is always preferable not to use GET method if a user is having some sensitive information or id/password to be sent to the server.

The QUERY_STRING is used to access the data which has been send.
The PHP provides $_GET associative array to access all the information using GET method.

Example

Code

<?php
if( $_GET[“name”] || $_GET[“age”] )
{
echo “Welcome “. $_GET[‘name’]. “<br />”;
echo “Your age is “. $_GET[‘age’]. ” years old.”;
exit();
}
?>
<html>
<body>
<form action=”<?php $_PHP_SELF ?>” method=”GET”>
Name: <input type=”text” name=”name” />
Age: <input type=”text” name=”age” />
<input type=”submit” />
</form>
</body>
</html>

The POST Method

The POST method transfers information through HTTP headers. The POST method does not have any restriction on data size to be sent.The POST method can be used to send ASCII as well as binary data.The POST Method is more secure as the data is not visible.

The PHP provides $_POST associative array to access all the sent information using POST method.

Example

Code

<?php
if( $_POST[“name”] || $_POST[“age”] )
{
echo “Welcome “. $_POST[‘name’]. “<br />”;
echo “Your age is “. $_POST[‘age’]. ” years old.”;
exit();
}
?>
<html>
<body>
<form action=”<?php $_PHP_SELF ?>” method=”POST”>
Name: <input type=”text” name=”name” />
Age: <input type=”text” name=”age” />
<input type=”submit” />
</form>
</body>
</html>

Scroll to Top