php

What is $_POST in PHP

When working with web forms and user input in PHP, you may come across the term $_POST. But what exactly is $_POST? In simple words, $_POST is a super global variable in PHP that is used to collect data submitted through an HTML form via the HTTP POST method. It enables the developers to access the values of form fields and process them in your PHP code.

This article will introduce $_POST and explain its usage with examples to help you grasp its concept easily.

What is PHP $_POST

The PHP $_POST is a super global variable that allows users to transmit form data upon submission of an HTML form. It plays a role in passing variables through the usage of the post method. The form’s input field data can be accessed using this super global variable. The response is transmitted via HTTP headers, and the security of the transmitted information relies on the HTTP protocol. To protect the privacy and security of your data, it is essential to employ secure HTTP. There are no limitations on the size of data that can be sent, and both ASCII and binary data can be transmitted.

The syntax for using the $_POST variable in PHP is given below.

$_POST['name']

The $_POST variable is an associative array, and you can access specific values by specifying the corresponding key within square brackets. In this case, the ‘name’ field is accessed using $_POST[‘name’].

Example 1

In the following example, there are two input fields, one is named, and the other is an email with submit button. We have used the variable $_POST to collect the input data of the field.

<!DOCTYPE html>
<html>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  Name: <input type="text" name="fname">
  Email: <input type="text" name="femail">
  <input type="submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  // collect value of input fields
  $name = $_POST['fname'];
  $email = $_POST['femail'];

  if (empty($name) && empty($email)) {
    echo "Name and Email are empty";
  } elseif (empty($name)) {
    echo "Name is empty";
  } elseif (empty($email)) {
    echo "Email is empty";
  } else {
    echo "Name: " . $name . "<br>";
    echo "Email: " . $email;
  }
}
?>

</body>
</html>

The provided code features an HTML form that collects user input for name and email, utilizing the POST method for data submission. It retrieves the values from the input fields (fname for name and femail for email) and validates if they are empty. It provides appropriate error messages if either field is empty, and if both fields have values, it displays the entered name and email.

When the user entered the data in the fields, it will produce the following results:

Example 2

Consider the following example of taking a name and gender as input in a form using the POST method in PHP and displaying the results.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
   if ($_POST["name"] || $_POST["gender"]) {
      if (preg_match("/[^A-Za-z'-]/", $_POST['name'])) {
        die("Invalid name. Name should contain only alphabets.");
      }
      echo "Welcome " . $_POST['name'] . "<br />";
      echo "You are " . $_POST['gender'] . ".";
      exit();
   }
}
?>
<html>
  <body>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
      Name: <input type="text" name="name" />
      Gender: <input type="text" name="gender" />
      <input type="submit" />
    </form>
  </body>
</html>

The given code first checks if the server request method is POST. If so, it proceeds to validate the name and gender fields. If the name field contains any characters other than alphabets, it displays an error message and terminates. Otherwise, it displays a welcome message with the entered name and the specified gender. The HTML section contains a form that allows users to input their name and gender, which is submitted using the PHP_SELF server variable.

The below snapshot is the output after adding the inputs and submitting the form.

Bottom Line

$_POST is a super global variable in PHP that enables the collection of data submitted through HTML forms using the POST method. It is crucial to understand and utilize $_POST effectively for securely handling user input in PHP applications. The provided examples illustrate how to access and process form field values using $_POST, demonstrating its practical usage. By utilizing $_POST correctly, you can ensure the secure handling of user data in your PHP applications.

About the author

Zainab Rehman

I'm an author by profession. My interest in the internet world motivates me to write for Linux Hint and I'm here to share my knowledge with others.