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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.