0
<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title> calculator </title>
 </head>
 <body bgcolor="goldenrod">

<center>
<h1>Calculator Application</h1>

<form action="projectcal.php" method="POST">

Type value1:<br/><input type="text" name="value1"><br/><br/>
Type value2:<br/><input type="text" name="value2"><br/><br/>
Calculation sign:<br/><input type="text" name="action"><br/><br/>
<input type="submit" name="submit" value="Submit">

</form>
</center>

above..created html form for putting the value...and below is the source code..

<?php

if(isset($_POST['submit']))
{   

storing in local var

  $value_1=$_POST['value1'];
  $value_2=$_POST['value2'];
  $action=$_POST['action'];
}         

if($value_1=='' || $value_2=='')                //errorline
{
   echo "<script>alert('Please Enter Any Value')</script>";   //if user enters blank value
   exit();
   }

   if($action=='')
   {
    echo "<script>alert('Please Enter the Operator Sign')</script>"; 
   }  

using array for shorter code

   if(in_array($action,array('+','-','*','/')))     

if user enters any of the the above sign in action field..than it will echo the value....and i am getting the result by using this ...but error is not going from line 29

   switch($action)
    {  
     case '+': 
       echo "Your answer is ";   
       echo $value_1+$value_2;
     break;

     case '-': 
       echo "Your answer is ";
       echo $value_1-$value_2;
     break;

     case '*':
       echo "Your answer is ";
       echo $value_1*$value_2;
     break;

     case '/':
       echo "Your answer is ";
       echo $value_1/$value_2;
     break;   
  }
 ?>


</body>
</html>

hope its clear now

  • Your error does not seem to match your code. Are you sure you pasted what you are running? – Andy Feb 03 '17 at 21:02
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – M. Eriksson Feb 03 '17 at 21:05
  • It looks like you're only setting that variable if that isset returns true (which it doesn't seem to do), but that you still are trying to use the variables later on, either way. – M. Eriksson Feb 03 '17 at 21:08
  • yes i pasted the code that i am running...for more information i have added some point...and whole code..@Andy C –  Feb 04 '17 at 10:10
  • .......i am setting that if user enters any numbers in value1 and value 2 ..user can enter any value for calculation. @MagnusEriksson ....and i am new to programming.my 1st yr got this work to do from college.. –  Feb 04 '17 at 10:17
  • thanks guys ...i didnt initialize the var...i got it....thanks for replying.. –  Feb 04 '17 at 10:31

2 Answers2

-1

You don't have a value in $_POST[value1]. Run

var_dump($_POST);  

to your screen to debug that issue. It'll show you the variables you are missing. I'd also recommend you review your use case.

Dom DaFonte
  • 1,619
  • 14
  • 31
  • I Created a view and controller to handle your code and I don't get any errors. I tested it with null values in one, or all fields, I tested it with the actual content and I dumped the $_POST variables to screen and it's working fine. – Dom DaFonte Feb 04 '17 at 13:39
-1

May be you forget to initialize the variable which use in your file.

//initialize the variable with your desire value 
$value_1 = '';
$value_2 = '';
$action = '';

try this code at the beginning of your file. i hope it'll solve your problem.

Nahid Hasan
  • 461
  • 2
  • 11