post method in php example

PHP POST Method 

Understand the use of PHP POST Method in 2 ways..

  1. In 2 pages (Request & Response page)
  2. In single page

 

In 2 pages (Request & Response page)

 

Request.php

<html>

<body>

<form method="POST" action="Response.php">

  First Name: <input type="text" name="FName"><br/>

  Last Name: <input type="text" name="LName"><br/>

  <input type="submit" id=”SubmitBTN” value=”SUBMIT”>

</form>

</body>

</html>

 

Response.php

<?php

 if ($_SERVER["REQUEST_METHOD"] == "POST")

{

$FirstName=$_POST[“FName”];

$LastName=$_POST[“LName”];

?>

<html>

<body>

  First Name:  <?php echo $FirstName;?><br/>

  Last Name:  <?php echo $LastName;?><br/>

</body>

</html>

<?php }

else

{

echo “Something Wrong”;

}

?>

 

 

In Single Page

 

SinglePage.php

<html>

<body>

<form method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>">

  First Name: <input type="text" name="FName"><br/>

  Last Name: <input type="text" name="LName"><br/>

  <input type="submit" id=”SubmitBTN” value=”SUBMIT”>

</form>

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

  $FirstName = $_POST[“FName”];

  $LastName=$_POST[“LName”];

 if (!empty($FirstName)) 

{

    echo "First Name is -".$FirstName;

}  

if (!empty($LastName))

{

    echo "<br/>Last Name is -".$LastName;

}

}

?>

</body>

</html>