-2
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<link rel="stylesheet" href="css/bootstrap.min.css">
<title>Submitted</title>
</head>

<body>
<?php
$employeeName = $_POST["txt_name"];
$employeeEmail = $_POST["txt_email"];
$employeeCompany = $_POST["txt_cmp"];
$employeeDesignation = $_POST["slc_dsgn"];
$employeeSalary = $_POST["txt_slry"];
$employeeStatus = $_POST["status"];
$employeeReason = $_POST["msg_rsn"];


?>
<div class="container">
<table class="table table-inverse">
 <thead>
<tr>
  <th>#</th>
  <th>Employee Name</th>
  <th>Email Address</th>
  <th>Company</th>
  <th>Designation</th>
  <th>Salary</th>
  <th>Status</th>
  </tr>
 </thead>
 <tbody>
  <tr>
    <th scope="row">1</th>
    <td><?php echo($employeeName)?></td>
  <td><?php echo($employeeEmail)?></td>
  <td><?php echo($employeeCompany)?></td>
  <td><?php echo($employeeDesignation)?></td>
  <td><?php echo($employeeSalary)?></td>
  <td><?php echo($employeeStatus)?></td>
</tr>

 </tbody>
 </table>
 <?php
 if($employeeSalary > 20000){
 $percentage = 35;
  $loan = ($percentage / 100) * $employeeSalary;

  }
  ?>

  <?php
  if($employeeSalary > 30000){
  $percentage = 25;
   $loan = ($percentage / 100) * $employeeSalary;

  }
  ?>

  <?php
  if($employeeSalary > 40000){
  $percentage = 15;
  $loan = ($percentage / 100) * $employeeSalary;

  }
  ?>
   <div class="text-center display-4 bg-faded justify-content-around"> your Loan will be <?php echo($loan)?> as per your current salary </div>

   <p class="blockquote text-center justify-content-around"> <?php echo($employeeReason);?>
   <span class="blockquote-footer">Reason</span>
   </p>
   </div>
   </html>

this is my code i am little new to php when i open my page on local host it say undefine variable of $percentage even i am defining it on each and every single if condition and later then i am using it to get percentage of sallary but i have also tried to put that variable globally still it not changing it value for my conditions I do not know how to sort this out

P.s this question is so on change i have tried alot to find similar one but i could not get.

Kamran Hassan
  • 43
  • 1
  • 10
  • where is `$_POST` coming from? – Rotimi Apr 21 '17 at 11:00
  • On what line do you get this error? – Jerodev Apr 21 '17 at 11:01
  • 1
    You will get this error when `$employeeSalary` is lower or equals to 20000 – Tobias F. Apr 21 '17 at 11:01
  • @mplungjan whoops, fixed. – Tobias F. Apr 21 '17 at 11:02
  • What if the `employer salary` is less than `20000`? you code fails here. Better you define `$percentage` before all conditions – masterFly Apr 21 '17 at 11:03
  • $_POST is coming from loan_info page which is working perfect even my submit page also working perfect i am getting each and every single value dynamically just when i want to get $percentage of sallary i want it like it different for different salaries. and error is in line 73 – Kamran Hassan Apr 21 '17 at 11:05
  • even if i define it before still when i change it in conditions it not getting change $percentage value it will be 35 all over in each and every single case even i declare it everytime differently. – Kamran Hassan Apr 21 '17 at 11:09
  • 1
    So you want to be the percantage behave like the following based on the employer salary (i will use `x` for keeping it short):`if (x <= 30000)->percentage = 35`,`if (x > 30000)->percentage = 25` and `if (x > 40000)->percentage = 15` – Tobias F. Apr 21 '17 at 11:14
  • Exactly i want it as you mentioned @tobias but i am facing error on line number 73 of condition of undefine variable even i have defined it. – Kamran Hassan Apr 21 '17 at 11:17
  • 1
    imagine the follwing: guy A earns 15.000$ now you check 3 things: 1.) is it greater than 20.000$?->NO Is it greater than 30.000$->NO Is it greater than 40.000$->NO So if none of this Conditions are met, your `$percentage` won't be defined as it is always within an `if`statement. So the Solution is quite simple: Remove the first `if`statement and before the two other divs you define `$percentage = 35`, so whenever the value is **NOT** greater than 30.000 or 40.000, you will get an percentage of 35. also you can move the calculation at the very end of your file. – Tobias F. Apr 21 '17 at 11:22
  • 1
    The problematic part of the code will look like(imagine`\n` as a linebreak): `$percentage = 35;\n if ($employeeSalary > 30000){$percentage = 25;}\n elseif($employeeSalary > 40000){$percentage = 15;}\n $loan = ($percentage / 100) * $employeeSalary;` After that you can use `$loan` as planned. – Tobias F. Apr 21 '17 at 11:26
  • I knew it that is why i used smart technique in form of loan i used drop down in which i specified your sallary 20000 to 30000 or 30000 to 40000 or 40000 to 50000 and given values on it as i want them for my conditions, this if is working if i define variable globally out of if condition just example if i do it like i put $percentage = 35; globally then whatever user will choose his salary it will give percentage of 35 not as i want 25 or 20 for different salaries that is my issue in my program – Kamran Hassan Apr 21 '17 at 11:27
  • 1
    have a look [here](https://eval.in/780187), in this example it works like it should. Maybe `$employeeSalary` is not number/ not defined? – Tobias F. Apr 21 '17 at 11:39
  • Thanks man you solved it Tobias, Thumbs up put that statement in comment so i can make this questions as answered – Kamran Hassan Apr 21 '17 at 11:42
  • 1
    Sorry i can't, @u_mulder closed this question, so the only thing you could do at the moment is upvoting my comment :( – Tobias F. Apr 21 '17 at 11:46

1 Answers1

0

Try conditionally defining it like this:

if(!isset($percent)){
  $percent = 0;
}

That will safely define the variable, but not upset anything in the program.

  • it not just about to define it variable also i want if user salary is 20000 then he get 35% loan if its more it get less as i define it but unfortunately it not letting me to define variable value it keeps show error undefine variable – Kamran Hassan Apr 21 '17 at 11:07