Home >>PHP Tutorial >Add two textbox values and display the result in third textbox

Add two textbox values and display the result in third textbox

Add two textbox values and display the sum in  third textbox using extract method

Enter two numbers and display the output in third text box when u click on submit button. Keep in mind one thing First and second text values doesn't reset. Result text box should not writable.

PHP Script

<?php
extract($_POST);
//do addition and store the result in $res
if(isset($add))
{
$res=$fnum+$snum;
}	
 
?>

HTML Form

<html>
<head>
	<title>Display the result in 3rd text box</title>
</head>
<body>
<form method="post">
 <table align="center" border="1">
	<Tr>
	<th>Your Result</th>
	<td><input type="text" readonly="readonly" value="<?php echo @$res;?>"/></td>
	</tr>
	<tr>
		<th>Enter first number</th>
		<td><input type="text" name="fnum" value="<?php echo @$fnum;?>"/></td>
	</tr>	
	<tr>
		<th>Enter second number</th>
		<td><input type="text" name="snum" value="<?php echo @$snum;?>"/></td>
	</tr>
	<tr>
		<td align="center" colspan="2">
		<input type="submit" value="+" name="add"/>
		
	</tr>
	</table>	
</form>
</body>
</html>

No Sidebar ads