PHP Superglobals POST

Table of Contents

PHP 1 $_POST in HTML Forms

A HTML form submits information via the HTTP POST method if the form's method attribute is set to "POST".

To demonstrate this, we start by creating a simple HTML form:

<h2>POST_in_HTML_Forms</h2> <p>HTML Form (file1.php)</p> <form method="POST" action="demo_request.php"> Name: <input type="text" name="fname"> <input type="submit"> </form>

Example 1

Result View Example

PHP 2 HTML form and PHP code in the same PHP file

<h2>HTML_form_and_PHP_code_in_the_same_PHP_file</h2> <p>In the example below we have put the HTML form and PHP code in the same PHP file. <br> We have also added some extra lines for security. </p> <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; } } ?>

Example 2

Result View Example

Document

Document in project

You can Download PDF file.

Reference