0

So, I am making a salary calculator Using Switch Statement to give value to $NetS variable, but when I try to echo in the textbox, it gives this error:

Undefined variable: NetS in C:\wamp\www\salary.php on line 73 Call Stack #TimeMemoryFunctionLocation 10.0003390392{main}( )...\salary.php:0 " />

Here is my code

<body>
<?php
if(isset($_POST['Submit']))
{
    $Dept = $_POST['Dept'];
    $ES = $_POST['ES'];
    $NetS;

    switch($Dept)
    {
        case "da":
        $NetS = $ES - ($ES*1/100)-500;
        break; 

        case "db":
        $NetS = $ES - ($ES*2/100)-500;
        break;

        case "dc":
        $NetS = $ES - ($ES*3/100)-500;
        break;

        case "dd":
        $NetS = $ES - ($ES*5/100)-500;
        break;

        default: echo"Invalid Choice or error";
        
        break;

    
    }
}
?> 

<h1 style="text-align:center;">CS310 Open Source Web Application Development</h1>   

<h3 style="text-align:center;" >Assignment no 1</h3>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="POST">
<br><br>
Select Department
<select name="Dept" id="Dept" class="form-control" style="width:230px">
<option value="">Select Options</option>
<option value="da">Department A</option>
<option value="db">Department B</option>
<option value="dc">Department C</option>
<option value="dd">Department D</option>
</select>
</form>
<br><br>
Employee Salary
<input type="text" name="ES" Placeholder="Enter Employee Salary" class="form-control" style="width:230px"/>
<br>
<input type="submit" name="Submit" class="tn btn-success" style="width:120px"/>
<br> <br>
Net salary
<input type="text" class="form-control" style="width:230px" name="NetSalary" value="<?php echo $NetS; ?>" />
</body>
</html>
  • Not your current issue but your `default` doesn't assign a value, it just outputs. `$NetS` only gets created when the form is submitted.. otherwise it is undefined. – user3783243 May 18 '21 at 18:45
  • Also if `department` value is not used `$_POST['ES'] - ( $_POST['ES'] * $_POST['Dept']/100) - 500` would be much easier, and just put the values in the `value`. Could do a `in_array` for the 4 values as well if want to throw `Invalid Choice or error` when not present. – user3783243 May 18 '21 at 18:49

1 Answers1

0

I have made some changes in your code:

1- Declare variable $NetS before " if(isset($_POST['Submit'])) "
2- Add submit button in form tag currently it outside form tag.
// Add proper body and html tags
// for submitting values to same page data I added "#" in action (change if you want)

Here is final code:

<?php
     $NetS = 0;
     if(isset($_POST['Submit']))
     {
         $Dept = $_POST['Dept'];
         $ES = $_POST['ES'];
         
     
         switch($Dept)
         {
             case "da":
             $NetS = $ES - ($ES*1/100)-500;
             break; 
     
             case "db":
             $NetS = $ES - ($ES*2/100)-500;
             break;
     
             case "dc":
             $NetS = $ES - ($ES*3/100)-500;
             break;
     
             case "dd":
             $NetS = $ES - ($ES*5/100)-500;
             break;
     
             default: echo"Invalid Choice or error";
             
             break;
     
         
         }
     }
     ?> 

<h1 style="text-align:center;">CS310 Open Source Web Application Development</h1>
  <h3 style="text-align:center;" >Assignment no 1</h3>
  <form action="#" method="POST">
     <br><br>
     Select Department
     <select name="Dept" id="Dept" class="form-control" style="width:230px">
        <option value="">Select Options</option>
        <option value="da">Department A</option>
        <option value="db">Department B</option>
        <option value="dc">Department C</option>
        <option value="dd">Department D</option>
     </select>
     <br><br>
     Employee Salary
     <input type="text" name="ES" Placeholder="Enter Employee Salary" class="form-control" style="width:230px"/>
     <br>
     <input type="submit" name="Submit" class="tn btn-success" style="width:120px"/>
  </form>
  <br> <br>
  Net salary
  <input type="text" class="form-control" style="width:230px" name="NetSalary" value="<?php echo $NetS; ?>" />

Output:

enter image description here

Faizan Ali
  • 297
  • 2
  • 9