PHP: Add two input box values and sum in third box

Sum of two variables is very easy but if we want to add two input box values and show the sum value in the third box it will be small difficult.

First of all, we will create an HTML form. In the HTML form , we will create two input box first input box for first input value and the second input box for second input value and will create a submit button. Whenever we will click on submit button the sum of values will print(display) in the third input box.

Add two numbers and result in the text box on the same page using PHP

Adding two numbers is a very easy task. Addition of two numbers and result value in the third box. We use HTML form and PHP script. Use mathematical logic of adding two numbers and convert them into programming. First of all, create an HTML form. The HTML form contains three input boxes and one button. When a user fills the number in the first two input boxes and clicks on the submit button then he gets the sum of two numbers. The user gets results on the same page.

HTML form

<html>
<body>
<form action="" method="post">
First Value:

Second Value :

</form> </body> </html>

In this above HTML form, we use the POST method. With the help of the POST method, we transfer information to the server. In the previous PHP tutorials, we discussed the POST method in PHP and GET method in PHP. With the help of the POST or GET method, we handle the HTML form information. This information can be added, multiply, subtraction and divide. Here, we add two numbers in another variable.

Now we will create a PHP script for the addition of two values and will get a result in third box on the same page .

PHP Script

<?php
$sum="";
if(isset($_POST['submit']))
{
	$no1=$_POST['no1'];
	$no2=$_POST['no2'];
	$sum=$no1+$no2;
	
		
}
Result:	
?>

Now this time to implementation. The file executed code is here .

Final Code

<html>
<body>
<form action="" method="post">
First Value:

Second Value :

</form> </body> </html> <?php $sum=""; if(isset($_POST['submit'])) { $no1=$_POST['no1']; $no2=$_POST['no2']; $sum=$no1+$no2; } ?> Result:
Run

Enter the numbers and click on the submit button. You will get the result in another input box.

Recommended Posts:-