Jump to content

[SOLVED] cant get this php calculator code to work.


silverglade

Recommended Posts

hi, here is the error i get

 

Notice: Undefined variable: val1 in D:\Inetpub\virtual\brendansite1\WWWROOT\calc.php on line 4

Invalid entry - please retry

 

here is the form code

 

<html><head> <title>Calculation Form</title> </head>

<body>
<form action="calc.php" method="post">
Value 1: <input type="text" name="val1" size="10">
Value 2: <input type="text" name="val2" size="10"> <br>
Calculation: <br>
<input type="radio" name="calc" value="add">Add
<input type="radio" name="calc" value="sub">Subtract
<input type="radio" name="calc" value="mul">Multiply
<input type="radio" name="calc" value="div">Divide <br>
<input type="submit" value="Calculate">
<input type="reset" value="Clear">
</form>
</body></html>

 

and here is the php code

 

 <html><head><title>Calculation Result</title></head> 
<body> 
<?php 
if( is_numeric($val1) && is_numeric($val2) ) 
{ 
   if($calc != null) 
   { 
        switch($calc) 
        { 
             case "add" : $result= $val1 + $val2; break; 
             case "sub" : $result= $val1 - $val2; break; 
             case "mul" : $result= $val1 * $val2; break; 
             case "div" : $result= $val1 / $val2; break; 
        } 
        echo("Calculation result: $result"); 
    } 
} 
else{ echo("Invalid entry - please retry"); } 
?> 
</body></html>  

 

any help greatly appreciated, im lost on this one. thanks. derek

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.