Click here to Skip to main content
16,021,211 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to calculate the sum of two numbers with php.
But my code can't do that.
Can somebody help me ? I'm a beginner .

XML
<!doctype html>
<html>
<head>

    <title>question2</title>
<head>

<body>
<?php
$num1=_$POST["num1"];
$num2=_$POST["num2"];
$result=$num1+$num2;
?>

<form action="test.htm" method="post" name="myform" align="center">
Enter number 1:<input type="text" name="num1" />
<br>
Enter number 2:<input type="text" name="num2" />
<br>
<input type="submit" name="result" />
</form>
<hr>

<table border='1' align='center'>

<td>number 1</td><td>calculate</td><td>number 2</td><td>result</td>
<tr>
<td>
<? echo  $num1; ?>
</td>
<td>+</td>
<td>
<? echo  $num2; ?>
</td>
<td>
<? echo  $result; ?> </td>

</table>

</body>
</html>
Posted
Updated 7-Aug-18 20:00pm

The soulution to your problem is as follows:
HTML
<html>
<head>
    <title>question2</title>
<head>
<body>
if(isset($_POST['num1'])){
   $num1 = $_POST['num1'];
   $num2 = $_POST['num2'];
   $result = $num1 + $num2;
}
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post" name="myform" align="center">
Enter number 1:<input type="text" name="num1" />
<br />
Enter number 2:<input type="text" name="num2" />
<br />
<input type="submit" name="result" />
</form>
<hr />

<table border="1" align="left"> 
	<tr><td align="center">number 1</td><td align="center">calculate</td><td align="center">number 2</td><td align="center">result</td></tr>
	<tr><td align="center"></td><td align="center">+</td><td align="center"></td><td align="center"></td></tr>
</table>
<?php } ??>

</head></head></html>

Have fun!!!
 
Share this answer
 
Comments
陳建勳 13-Aug-12 9:45am    
Hello,Patrick Wanjau!
I have a little problem.
SERVER['PHP_SELF']
what is its effect ?
Patrick Wanjau 13-Aug-12 9:51am    
It redirects to the same url. For example, if you have www.site.com, it will redirect to www.site.com, if you have www.site.com/index.php - redirect to www.site.com/index.php...etc
I never done anything with PHP, but the code in this post How to add two numbers using PHP[^] looks like it might be helpful.
 
Share this answer
 
Comments
陳建勳 12-Aug-12 13:04pm    
thank you ! This is so helpful.

but I found a new problem!!!...
My code is correct but it can't be executed by my browser.
only showed off the part of HTML .
enhzflep 12-Aug-12 14:39pm    
The thing with PHP is, it ISN'T executed in your browser, but rather in the server - it's a Server Side language.
You'll need to install a webserver to use this code.

I recommend XAMPP - It's free, cross-platform and works.
陳建勳 13-Aug-12 0:02am    
Thank you
I have installed the Appserv v2.5.10,so it is not cause of the problem,
and it's bizarre! problem was solved by itself in today's morning!!
enhzflep 13-Aug-12 0:09am    
You're welcome. Great! Glad it works now.
Strange problem...
I don't know what happen.
but the problem have been solved by itself in today's morning!!
Perhaps associated with server Maintenance.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900