Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

$_POST

PHP $_POST

$_POST contains an array of variables received via the HTTP POST method.

Variables can be sent using the HTTP POST method in two main ways:

  1. HTML forms
  2. JavaScript HTTP requests

$_POST in HTML Forms

An HTML form submits information via the HTTP POST method if the form’s method attribute is set to “POST”.

To illustrate this, let’s create a simple HTML form:

HTML Form

<html>
<body>
<form method="POST" action="demo_request.php">
  Name: <input type="text" name="fname">
  <input type="submit">
</form>
</body>
</html>

When a user clicks the submit button, the form data is sent to a PHP file specified in the action attribute of the <form> tag.

In the specified action file, we can use the $_POST variable to collect the values of the input fields.

PHP file

$name = $_POST['fname'];
echo $name;

 

In the example below, we have included both the HTML form and PHP code in the same PHP file.

Additionally, we have added some extra lines for security.

Example

<html>
<body>

<
form method="POST"
action="<?php echo $_SERVER['PHP_SELF'];?>">
  Name: <input type="text" name="fname">
 <input type="submit">
</form>
 
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $name = htmlspecialchars($_POST['fname']);
 if (empty($name)) {
    echo "Name is empty";
 } else {
    echo $name;
 }
}
?>
</body>
</html>

$_POST in JavaScript HTTP Requests

When sending an HTTP request in JavaScript, you can specify that the HTTP method is POST.

To demonstrate this, we start by creating a JavaScript function that contains an HTTP request:

JavaScript function

function myfunction() {
 const xhttp = new XMLHttpRequest();
  xhttp.open("POST", "demo_phpfile.php");
  xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xhttp.onload = function() {
    document.getElementById("demo").innerHTML = this.responseText;
 }
  xhttp.send("fname=Mary");
 }
}

The code above will:

  • Initiate an HTTP request
  • Set the HTTP method to POST
  • Set a valid request header
  • Create a function to execute when the request is done
  • Send the HTTP request with a variable fname set to “Mary”

Now, let’s examine the function that will be executed when the request is done:

xhttp.onload = function() {
    document.getElementById("demo").innerHTML = this.responseText;
 }

The function will try to write the response from the operation into an HTML element with id="demo".

Let’s create an HTML page with such an element, and a button that executes the function.

Including the JavaScript, the page will look like this:

Example

How to send and receive data using an HTTP request:

<html>
<script>
function myfunction() {
 const xhttp = new XMLHttpRequest();
  xhttp.open("POST", "demo_ajax.php");
  xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xhttp.onload = function() {
    document.getElementById("demo").innerHTML = this.responseText;
 }
  xhttp.send("fname=Mary");
 }
}
</script>
<body>

<
button onclick="myfunction()">
Click me!</button>

<
h1 id="demo"></h1>

</
body>
</html>

In the PHP file that receives this HTTP request (demo_ajax.php), we simply use the $_POST variable to retrieve the fname variable and send it as a response.

PHP file

$name = $_POST['fname'];
echo $name;