In this article POST method in PHP Examples we give the simple, conditional and looping examples with html form and php code.
POST method in PHP Examples
# 1. Write a PHP Program to find addition of two numbers
# Addition.html
<html>
<head>
<title>Addition of Two Numbers</title>
</head>
<body>
<form action=”Sum2.php” method=”post”>
Number 1:
<input type=”text” name=”num1″><br><br>
Number 2:
<input type=”text” name=”num2″><br><br>
<input type=”submit” value=”Addition”>
</form>
</body>
</html>
# Sum2.php
<?php
$a=$_POST[‘num1’];
$b=$_POST[‘num2’];
echo “Addition of two number: “,$a+$b;
?>
# 2. Write a PHP Program to find Area of rectangle
# AreaInput.html
<html>
<head>
<title>Area of Rectangle</title>
</head>
<body>
<form name=”Form1″ method=”get” action=”Area_Rect.php”>
Number 1: <input name=”Length” type=”text”>
<br><br>
Number 2: <input name=”Breadth” type=”text”>
<br><br>
<input type=”submit” value=”Area”>
</form>
</body>
</html>
# Area_Rect.php
<?php
$l=$_GET[‘Length’];
$b=$_GET[‘Breadth’];
echo “Area of rectangle: “,$l*$b;
?>
# 3. Write a PHP program to show given number is Odd/Even
#OddEvenNum.html
<HTML>
<head>
<title>ODD/EVEN Number</title>
</head>
<body>
<form action=”OddEven.php” method=”post”>
Number 1:
<input type=”text” name=”num1″><br><br>
<input value=”Result” type=”submit”></button>
</form>
</body>
</html>
# OddEven.php
<?php
$a=$_POST[‘num1’];
if($a%2==0)
echo “$a is Even Number.”;
else
echo “$a is Odd Number.”;
?>
# 4. Write a PHP program to find largest number among two numbers.
# Max2.html
<html>
<head>
<title>Find Largest Number</title>
</head>
<body>
<form action=”FindMax.php” method=”post”>
Number 1:
<input type=”text” name=”num1″><br><br>
Number 2:
<input type=”text” name=”num2″><br><br>
<input type=”submit” value=”Max”>
</form>
</body>
</html>
# FindMax.php
<?php
$a=$_POST[‘num1’];
$b=$_POST[‘num2’];
if($a>$b)
echo “$a is largest Number.”;
else
if($b>$a)
echo “$b is largest Number.”;
else
echo “Both are equal.”;
?>
# 5. Write a PHP program to show sum of digit.
# SumDig.html
<HTML>
<head>
<title>Sum of Digit</title>
</head>
<body>
<form action=”Sum_Dig.php” method=”post”>
Number 1:
<input type=”text” name=”num1″><br><br>
<input value=”Result” type=”submit”></button>
</form>
</body>
</html>
# Sum_Dig.php
<?php
$a=$_POST[‘num1’];
$dig=0;
$sum=0;
while($a>0)
{
$dig=$a%10;
$sum=$sum+$dig;
$a=$a/10;
}
echo “Sum of digit: $sum”;
?>
# Write a PHP Program to show reverse number
RevNum.html
<HTML>
<head>
<title>Reverse Number</title>
</head>
<body>
<form action=”RevNumber.php” method=”post”>
Number 1:
<input type=”text” name=”num1″> <br><br>
<input value=”Result” type=”submit”></button>
</form>
</body>
</html>
RevNumber.php
<?php
$a=$_POST[‘num1’];
$dig=0;
$rev=0;
while($a>0) # 123 12 1
{
$dig=$a%10; # 3 2 1
$rev=($rev*10)+$dig; # 321
$a=(int)($a/10); # 12 1 0
}
echo “Reverse Number: $rev”;
?>
// Database Connectivity
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining