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

<form action=”index.php” method=”POST”>
    <br><br>
    Enter the First Name:
    <input type=”text” name=”fname” id=”fname”><br><br>
    Enter the Last Name:
    <input type=”text” name=”lname” id=”lname”><br><br>
    Enter the Address:
    <input type=”text” name=”address” id=”address”><br><br>
    <input type=”submit”>
</form>
<?php
    $servername=”localhost”;
    $username=”root”;
    $pwd=””;
    $db=”bca”;
    $conn=new mysqli($servername,$username,$pwd,$db);
    if($conn->connect_error)
    {
        die(“Connection fail”);
    }
    $fname=$_POST[‘fname’];
    $lname=$_POST[‘lname’];
    $address=$_POST[‘address’];
    $sql=”INSERT INTO `stud_info` (`RollNo`, `FName`, `LName`, `Address`) VALUES (NULL, ‘$fname’, ‘$lname’, ‘$address’)”;
    if($conn->query($sql)==true)
    {
        echo”Connection successful!”;
    }
    else
    {
        echo “No connection”;
    }
# Show the all data in the table using select command
    $sql=”SELECT * FROM `stud_info`”;
    $result=mysqli_query($conn,$sql);
    echo “<br>”;
    echo “Number of Rows: “;
    $num = mysqli_num_rows($result);
    echo “$num”;
    echo “<br>”;
    if($num>0)
    {
        while($row=mysqli_fetch_assoc($result))
        {
            echo “Roll Number: ” . $row[“RollNo”] . ” – First Name: ” . $row[“Fname”] . ” – Last Name: ” . $row[“LName”] . ” – Address: ” . $row[“Address”] ;
            echo “<br>”;
        }
    }
# update data in the table using update command
$sql2 = “UPDATE stud_info SET Address=’Sangli’ WHERE RollNo=4”;
if ($conn->query($sql2) === TRUE) {
    echo “<br>”;
    echo “Record updated successfully!”;
} else {
    echo “<br>”;
    echo “Error: ” .$conn->error;
}
# delete data in the table using delete command
$sql = “DELETE FROM stud_info WHERE RollNo=8”;
if ($conn->query($sql) === TRUE) {
    echo “<br>”;
    echo “Record deleted successfully!”;
} else {
    echo “<br>”;
    echo “Error: ” .$conn->error;
}
    $conn->close();
    ?>
Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *