-1

This might be a beginner question for some but I can't get through with this: See, what I have is a simple text box and submit button. The text box is required to be inputted by numbers (it should be the user's salary). When ever I load the php file in the any browser I got this error (see image below). What's in the line 35 is the code?

$a = $_POST['salary'];

I don't know what's wrong with that...

enter image description here

<form method="POST">

Salary: <input id="salarytext" type="text" name="salary" onkeypress="return isNumberKey(event)"><br>
<input type="submit" />
</form> 


<?php

$a = $_POST['salary'];

?>            

<?php
switch($a) {                        

    case ($a==""): echo "Input first your Compensation for the period!";
    break;

    case ($a < 1000 ): echo "Your Compensation is too low. You are not required to contribute for the period.";
    break;

    case ($a >=1000 && $a <=1249.99): echo "Your ER is 80.7 and Your EE is 33.30";
    break;

    case ($a >=1250 && $a <=1749.99): echo "Your ER is 116 and Your EE is 50.00";
    break;

    case ($a >=1750 && $a <=2249.99): echo "Your ER is 151.3 and Your EE is 66.70";
    break;

    case ($a >=2250 && $a <=2749.99): echo "Your ER is 186.7 and Your EE is 83.3";
    break;

    case ($a >=2750 && $a <=3249.99): echo "Your ER is 222 and Your EE is 100";
    break;

    case ($a >=3250 && $a <=3749.99): echo "Your ER is 257.3 and Your EE is 116.7";
    break;

    case ($a >=3750 && $a <=4249.99): echo "Your ER is 292.7 and Your EE is 133.3";
    break;

    case ($a >=4250 && $a <=4749.99): echo "Your ER is 328 and Your EE is 150";
    break;

    case ($a >=4750 && $a <=5249.99): echo "Your ER is 363.3 and Your EE is 166.7";
    break;

    case ($a >=5250 && $a <=5749.99): echo "Your ER is 398.7 and Your EE is 183.3";
    break;

    case ($a >=5750 && $a <=6249.99): echo "Your ER is 434 and Your EE is 200";
    break;

    case ($a >=6250 && $a <=6749.99): echo "Your ER is 469.3 and Your EE is 216.7";
    break;

    case ($a >=6750 && $a <=7249.99): echo "<span>Your ER is 504.7 and Your EE is 233.3 </span>". "with a total SSS Contribution of 738.00" ;
    break;

    case ($a >=7250 && $a <=7749.99): echo "Your ER is 540 and Your EE is 250";
    break;

    case ($a >=7750 && $a <=8249.99): echo "Your ER is 575.3 and Your EE is 266.7";
    break;

    case ($a >=8250 && $a <=8749.99): echo "Your ER is 610.7 and Your EE is 283.3";
    break;

    case ($a >=8750 && $a <=9249.99): echo "Your ER is 646 and Your EE is 300";
    break;

    case ($a >=9250 && $a <=9749.99): echo "Your ER is 681.3 and Your EE is 316.7";
    break;

    case ($a >=9750 && $a <=10249.99): echo "Your ER is 716.7 and Your EE is 333.3";
    break;

    case ($a >=1025 && $a <=10749.99): echo "Your ER is 752 and Your EE is 350";
    break;

    case ($a >=1075 && $a <=11249.99): echo "Your ER is 787.3 and Your EE is 366.7";
    break;

    case ($a >=1125 && $a <=11749.99): echo "Your ER is 822.7 and Your EE is 383.3";
    break;

    case ($a >=1175 && $a <=12249.99): echo "Your ER is 858 and Your EE is 400";
    break;

    case ($a >=1225 && $a <=12749.99): echo "Your ER is 893.3 and Your EE is 416.7";
    break;

    case ($a >=1275 && $a <=13249.99): echo "Your ER is 928.7 and Your EE is 433.3";
    break;

    case ($a >=1325 && $a <=13749.99): echo "Your ER is 964 and Your EE is 450";
    break;

    case ($a >=1375 && $a <=14249.99): echo "Your ER is 999.3 and Your EE is 466.7";
    break;

    case ($a >=1425 && $a <=14749.99): echo "Your ER is 1034.7 and Your EE is 483.3";
    break;

    case ($a >=1475 && $a <=30000): echo "Your ER is 1090 and Your EE is 500";
    break;

    default:
    echo " Your ER is 1090 and Your EE is 500. Your Salary for the Period reached the Maximum Range of Compensation. ";
    break;

}

?>

daksh21ubuntu
  • 276
  • 2
  • 4
  • 14
Kareen Lagasca
  • 909
  • 4
  • 19
  • 44
  • 6
    Check if `isset($_POST['salary'])` first. – elclanrs Jul 07 '13 at 07:34
  • 1
    BTW, you're using `switch` all wrong. These should be `if`/`elseif`s, or you could say `switch (true)` if you feel like twisting the language a bit. But right now, it's testing if, say, the boolean expression (`$a >= 1475 && $a <= 30000`) is equal to `$a` -- which doesn't make any sense at all, and should hardly ever be true unless PHP's doing some odd boolean coercion thing... – cHao Jul 07 '13 at 07:42
  • thanks @cHao for pointing that out. Well, you see, I just learned a very little PHP today (about 4 hrs ago) and tried to experiment. Then that's what i come up! What a mess right! :) – Kareen Lagasca Jul 07 '13 at 07:50
  • ...which, apparently, it is. PHP's type juggling is too clever for its own good sometimes. – cHao Jul 07 '13 at 07:50
  • BTW2: Looks like your minimums get a bit wonky past 9750. All those numbers should have 5 digits, methinks. – cHao Jul 07 '13 at 07:52
  • thank you for noticing that. – Kareen Lagasca Jul 07 '13 at 07:55

5 Answers5

5

A common idiom is to do this

 $a = isset($_POST['salary']) ?  $_POST['salary'] : '';

This will set $a to the posted value if set, and use a default if not.

Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
2

You need to check of the index is set. For example, before assigning your variable to $a, use:

$a = (isset($_POST['salary'])) ? $_POST['salary'] : '';

This uses a ternary operator (similar to an if-else block) but essentially, if $_POST['salary'] is indeed set, it will use that value, otherwise it falls back to a default of an empty string.

BenM
  • 52,573
  • 26
  • 113
  • 168
0

you should include your calculation inside a condition which does the following

$a = "";
if (isset($_POST['salary'])) {
    $a = $_POST['salary'];
}
DevZer0
  • 13,433
  • 7
  • 27
  • 51
0

You should use isset() method before you actually use your $_POST['salary'] to check if it is set.

Daniel Kmak
  • 18,164
  • 7
  • 66
  • 89
0

First check if $_POST has elements

if($_POST){
   $a = $_POST["salary"]; 
 } 
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
  • @NullPoiиteя: Except this one doesn't even have the good sense to check if the specific post variable is set. – cHao Jul 07 '13 at 07:55
  • whoops i edited ..even didn't notice that he is just checking weather the post is set or not ... – NullPoiиteя Jul 07 '13 at 07:56