I am typing a simple codes in PHP and HTML. How can I display the result in same page of browser not in another window tab or page?
Following is the simple HTML formula code:
To add two numbers and display on the same page
<html>
<head>
<title> Sum of two numbers </title>
<body>
<form action="sum.php" method="POST">
First Number: <input type="Text" Name="Num1"><br>
Second Number: <input type="Text" Name="Num2"><p>
<input type="Submit" value="Calculate">
</form>
</body>
</html>
and sum.php file is as follows.
<?php
// add First and Second Numbers
$sum = $_POST["Num1"] + $_POST["Num2"];
// their sum is diplayed as
echo "The sum of First Number (".$_POST["Num1"].") and
Second Number (".$_POST["Num2"].") is $sum";
?>
<html>
<head>
<title> Sum of two numbers </title>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
First Number: <input type="Text" Name="Num1"><br>
Second Number: <input type="Text" Name="Num2"><p>
<input type="Submit" value="Calculate">
</form>
<?php
if (count($_POST) > 0 && isset($_POST["Num1"]) && isset($_POST["Num2"])){
// add First and Second Numbers
$sum = $_POST["Num1"] + $_POST["Num2"];
// their sum is diplayed as
echo "The sum of First Number (".$_POST["Num1"].") and
Second Number (".$_POST["Num2"].") is $sum";
}
?>
</body>
</html>
You have to combine the PHP code and the HTML code in the same file.
Then you just set the forms action to the same page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With