Replacing form fields by text in PHP

In PHP, the form field has replaced with the custom text after processing by either using an HTML file or hiding the form fields using the PHP script. This method can also be used for applying static header and footer on the web page. In the following example, I have used an HTML file and PHP script to add two numbers. The HTML file content has replaced during the execution.

Before Execution

Addition of two numbers

After Execution

Output of Addition of two numbers

Source Code:

form.html

<form method="post" action="<?php print $_SERVER['PHP_SELF']; ?>" >
    First Number: &nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="first"><br>
    Second Number: <input type="text" name="second"><br><br>
    <input type="reset" value="Reset"><input type="submit" value="Submit">
</form>

addition.php

<html>
    <head>
        <title>Adding two numbers</title>
    </head>
    <body>
        <center>
        <br><br>
        <h2>Addition of two Numbers</h2>
        <hr width=40%>
        <?php
        if(!$_POST) {
            include("form.html");
        } else {
            $n1 = ($_POST["first"]);
            $n2 = ($_POST["second"]);
            $n3 = $n1 + $n2;
            print "<br>Addition of $n1 and $n2 is $n3 <br><br>";
        }
        ?>
        <hr width = 40%>
        </center>
    </body>
</html>

Note: You must save both files in the same directory, and run the PHP file through the webserver.

Comments

Most Popular Posts

TNEB Bill Calculator

TNEB Bill Calculator (New)

Technical Questions