Table of Contents
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>
Result View Example
<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;
}
}
?>
Result View Example
Document in project
You can Download PDF file.