PHP Form Handling : Post

The POST method is widely used in html form to submit large amount of data such as file upload, image upload, login form registration form etc. In POST method data is not sent as part of URL string to server, instead it is sent as part of message body. Post requests are used to make more complex requests on the server. For instance, if a user has filled a form with multiple fields and the application wants to save all the form data to the database.

The PHP provides superglobal variable $_POST to access all the information sent via post method or submitted through an HTML form using the method="post".  The POST method create an associative array, which holds the data in key/value pairs, where key are the names of the form fields and values are the input data from the user. The variable $_POST Contains a list of all the field names and values sent by form using the POST method, which can be accessed by the name of fields :
 $_POST['field_name'];
For example, we can access the above mentioned form data with the below code
 $_POST['name'];
 $_POST['age'];
Now lets see the below Example code :

form.php
<html>
<body>

  <form action="file.php" method="post">
    Name : <input type="text" name="name"/>
    Age : <input type="number" name="age"/>
    <input type="submit" value="Submit"/>
  </form>

</body>
</html>
file.php
<?php

  $name = $_POST['name'];
  $age = $_POST['age'];
  echo "Hello $name. <br/>";
  echo "You are $age years old."

?>
Output :



Use of POST Method :

Information sent from a form with the POST method is invisible to others. All names/values are embedded within the body of the HTTP request. It has no limits on the amount of information to send. The POST supports advanced functionality such as support for multi-part binary input while uploading files to server. However, because the variables are not displayed in the URL, it is not possible to bookmark the page.


Next Topic :