EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

PHP $_POST

Home » Software Development » Software Development Tutorials » PHP Tutorial » PHP $_POST

PHP $_POST

Introduction to PHP $_POST

PHP comes out a function of $_POST that is a super global variable that is used to collect the data. After submitting an HTML Form some data is generated which is collected using a specific method POST. $_POST is also used for passing variables. It is basically passed to the HTTP POST method as the content type in the request. This is a super variable/automatic global variable whose scope is available throughout the script that means there is no need to declare a global variable to access this. This method _POST is available after the stable release of PHP 4 and above.

Syntax: 

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

The Syntax for getting details using $_Post function is:

<?php
$_POST[‘name’] ?>
“$_POST[…]” is the PHP array
'name'” is the URL variable name.

Screenshot :

PHP $_POST 1

$_POST Variable Working

The $_POST variable collects the data from the HTML form via the POST method. When a user fills the data in a PHP form and submits the data that is sent can be collected with the _POST Method in PHP. The Post Method transfers the information via the Headers. The Information is encoded using a scheme where the KEY/VALUE pairs are joined with an equal sign. All the special characters are removed and the data is encoded. Once we get the encoded data it is sent to a header called QUERY_STRING.

It doesn’t have the limit of data size to be sent in this QUERY_STRING. Both Binary, as well as ASCII data, can be sent of with the POST method. The data is then passed with some security protocol making the information secured and then sent to an associative array using the $_POST method.

The data sent by the POST Method is invisible to others and we cannot see any form of data when passed on. _POST is an array of variable passed to script via the HTTP Post Method.

The time spent by method POST is more than that of the other request that is why it has some what low performance, But it is widely used as the exposure for this is more as it supports many data types such as String, numeric, binary. And since the Values are not visible the result cannot be book marked.

The Post method is used for creating complex Form in HTML and to handle complex data are sent over the network.

Popular Course in this category
PHP Training (5 Courses, 3 Project)5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,678 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

So after the URL is Hit the URL will be something like that with the POST Method:

Localhost/pagename.php

Examples of PHP $_POST

Let us see some functioning of $_POST with some Example:

Example #1

Let’s start by creating an HTML form and see how the Request Generates.

Method Used Post:

HTML FORM:

This is HTML form.

<form method="post" action="<?php echo “hello PHP”;?>">
Your Name: <input type="text" name="pname">
<input type="submit">
</form>

PHP POST Request Method:

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = htmlspecialchars($_REQUEST['sname']);
if (empty($name)) {
echo "Empty Name";
} else {
echo $name;
}
}
?>

We are trying to create a FORM that takes Input as the Name, a request is submitted from the method POST with certain details and the data is collected by the Request Method.

Here the htmlspecialchars($Request[‘fname’]) takes the value of Input Field and checks whether any condition is given over there or not. If any condition is there that is checked or else the data is collected. So the Output for this will be something like:

Screenshot:

PHP $_POST 2

Let us check some more example:

Let’s create a Form that takes data from two field Name and Email of a user and let’s collect that using the POST Method. We will create a PHP File called Demo.php

Here is the Code:

<html>
<body>
Hey the name is <?php echo $_POST["name"]; ?><br>
With the email address is: <?php echo $_POST["email"]; ?>
</body>
</html>

Create an HTML form with the Request with POST.

Code:

<html>
<body>
<form action="Demo.php" method="post">
The Name: <input type="text" name="Name"><br>
The E-mail: <input type="text" name="Email"><br>
<input type="submit">
</form>
</body>
</html>

Screenshot:

Output 3

When we request or run this PHP code the request is generated and data is sent for processing.

Example #2

Let us check one more simple Example:

<form action="Demo2.php" method="post">
<p>Add<input type="text" name="Add" /></p>
<p>City<input type="text" name="City" /></p>
<input type="submit" name="submit" value="Submit" />
</form>

Simple POST Request to get the Address and City

<?php
echo("Address: " . $_POST['Add'] . "<br />\\n");
echo("City " . $_POST[‘City'] . "<br />\\n");
?>

Screenshot:

Output 4

We can use the isset function to check whether the value for this variable has been set or not. We can use conditional statements also with this method.

Screenshot:

<?php
echo("Address: " . $_POST['Add'] . "<br />\\n");
echo("City " . $_POST['City'] . "<br />\\n");
?>
<form action="Demo2.php" method="post">
<p>Add<input type="text" name="Add" /></p>
<p>City<input type="text" name="City" /></p>
<input type="submit" name="submit" value="Submit" />
</form>

Conclusion

From the above article, we saw the use of Function $_POST in PHP. From various example and classification we tried to understand how the $_POST function works in PHP and what are is use in the programming level.

We also saw the internal working and the advantages of having the type of data which we are defining for various programming purpose. Also, the syntax and examples helped us to understand much precisely the function.

Recommended Articles

This is a guide to PHP $_POST. Here we discuss the introduction to PHP $_POST, How PHP $_POST works in PHP, and Examples with code implementation?. You can also go through our other suggested articles to learn more –

  1. PHP Stack Trace
  2. PHP Object to String
  3. PHP require_once
  4. PHP XML Parser

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PHP Tutorial
  • Functions
    • Functions in PHP
    • PHP Math Functions
    • PHP Recursive Function
    • PHP String Functions
    • Hashing Function in PHP
    • Date Function in PHP
    • PHP Anonymous Function
    • Calendar in PHP
    • PHP Call Function
    • PHP Pass by Reference
    • PHP ucfirst()
    • PHP ucwords()
    • trim() in PHP
    • isset() Function in PHP
    • PHP replace
    • PHP fpm
    • PHP strpos
    • preg_match in PHP
    • PHP preg_replace()
    • PHP ob_start()
    • PHP Reflection
    • PHP Split String
    • PHP URL
    • PHP preg_match_all
    • PHP strtoupper()
    • PHP preg_split()
    • PHP substr_replace()
    • PHP setlocale()
    • PHP substr_count()
    • PHP Serialize
    • PHP strlen()
    • PHP async
    • PHP Date Time Functions
    • PHP timezone
    • PHP Data Object
    • print_r() in PHP
    • PHP header()
    • PHP strip_tags()
    • PHP chop()
    • PHP MD5()
    • PHP unset()
    • PHP crypt()
    • PHP wordwrap()
    • PHP is_null()
    • PHP strtok()
    • PHP bin2hex()
    • PHP parse_str()
    • PHP levenshtein()
    • PHP addslashes()
    • PHP strtotime
    • PHP sha1()
    • PHP explode()
    • PHP sscanf()
    • PHP require_once
    • PHP Zip Files
    • PHP $_SERVER
    • PHP $_POST
    • PHP Include and Require
    • PHP POST Method
  • PHP Basic
    • Introduction To PHP
    • What is PHP
    • PHP Keywords
    • Advantages of PHP
    • Career In PHP
    • Comments in PHP
    • PHP Commands
    • PHP Frameworks
    • PHP Compiler
    • Variables in PHP
    • PHP Superglobal Variables
    • PHP Versions
    • Object in PHP
    • What is Drupal
    • Top PHP Frameworks
    • WebStorm IDE
    • What is phpMyAdmin?
    • PhpStorm
    • Install phpMyAdmin
    • Phalcon Model
  • Data Types
    • PHP Data Types
    • PHP Integer
    • PHP Booleans
  • Operators
    • PHP Operators
    • Arithmetic Operators in PHP
    • Comparison Operators in PHP
    • Logical Operators in PHP
    • Bitwise Operators in PHP
    • Ternary Operator in PHP
    • PHP String Operators
  • Control Statements
    • Control Statement in PHP
    • PHP if Statement
    • if else Statement in PHP
    • elseif in PHP
    • PHP Switch Statement
    • Continue in PHP
    • Break in PHP
  • Loops
    • PHP Loops
    • For Loop in PHP
    • PHP Do While Loop
    • PHP While Loop
    • While Loop in PHP
    • Foreach Loop in PHP
  • Constructor
    • Constructor in PHP
    • Destructor in PHP
  • State Management
    • Cookie in PHP
    • Sessions in PHP
  • Array
    • What is PHP Array
    • Arrays in PHP
    • 2D Arrays in PHP
    • Associative Array in PHP 
    • Multidimensional Array in PHP
    • Indexed Array in PHP
    • PHP Array Functions
    • PHP unset Array
    • PHP Append Array
    • PHP Array Search
    • PHP Split Array
    • PHP array_push()
    • PHP array_pop()
  • Advanced
    • Overloading in PHP
    • Overriding in PHP
    • Method Overloading in PHP
    • Inheritance in PHP
    • Multiple Inheritance in PHP
    • PHP Interface
    • Encapsulation in PHP
    • PHP Constants
    • PHP Magic Constants
    • PHP Regular Expressions
    • PHP GET Method
    • PHP Annotations
    • PHP Encryption
    • PHP file Functions
    • PHP readfile
    • PHP?Write File
    • PHP Append File
    • PHP Type Hinting
    • PHP Filters
    • PHP Float
    • PHP Form
    • PHP Form Validation
    • Sorting in PHP
    • PHP usort()
    • PHP Stack Trace
    • PHP Stack Overflow
    • PHP Pagination
    • PHP implode
    • Polymorphism in PHP
    • Abstract Class in PHP
    • PHP Final Class
    • PHP Custom Exception
    • error_reporting() in PHP
    • PHP Log Errors
    • Access Modifiers in PHP
    • PHP Change Date Format
    • Static Method in PHP
    • PHP File Handling
    • PHP Output Buffering
    • Get IP Address in PHP
    • Upload a File in PHP
    • String in PHP
    • Public Function in PHP
    • Private in PHP
    • Protected in PHP
    • basename in PHP
    • Validation in PHP
    • PHP mail()
    • PHP Email Form
    • PHP Directory
    • PHP Create Session
    • PHP include_once
    • PHP json_decode
    • PHP XMLWriter
    • PHP XML Reader
    • PHP XML Parser
    • PHP XML into Array
    • Phalcon Framework
  • Programs
    • Patterns in PHP
    • Star Patterns in PHP
    • Swapping in PHP
    • Fibonacci Series PHP
    • Factorial in PHP
    • Reverse String in PHP
    • Square Root in PHP
    • Random Number Generator in PHP
    • Palindrome in PHP
    • Prime Numbers in PHP
    • Armstrong Number in PHP
    • Socket Programming in PHP
    • Login Page in PHP
    • PHP Login Template
    • PHP Object to String
  • Database
    • PHP Database Connection
    • How to Connect Database to PHP
  • Interview Questions
    • PHP Interview Questions
    • PHP OOP Interview Questions
    • CakePHP Interview Questions
    • Core PHP Interview Questions

Related Courses

PHP Training Course

Java Servlet Training

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - PHP Training Course Learn More