-1

对于某些人来说,这可能是一个初学者的问题,但我无法解决这个问题:看,我所拥有的是一个简单的文本框和提交按钮。文本框要求输入数字(应该是用户的工资)。每当我在任何浏览器中加载 php 文件时,都会出现此错误(见下图)。第 35 行中的代码是什么?

$a = $_POST['salary'];

我不知道这有什么问题...

在此处输入图像描述

<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;

}

?>

4

5 回答 5

5

一个常见的习惯是这样做

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

如果设置,这将设置$a为发布的值,如果没有,则使用默认值。

于 2013-07-07T07:35:29.213 回答
2

您需要检查索引是否已设置。例如,在将变量分配给 之前$a,请使用:

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

这使用三元运算符(类似于 if-else 块),但本质上,如果$_POST['salary']确实设置了,它将使用该值,否则它会回退到默认的空字符串。

于 2013-07-07T07:35:47.470 回答
0

您应该将您的计算包含在执行以下操作的条件中

$a = "";
if (isset($_POST['salary'])) {
    $a = $_POST['salary'];
}
于 2013-07-07T07:36:00.477 回答
0

您应该isset()在实际使用您之前使用方法$_POST['salary']来检查它是否已设置。

于 2013-07-07T07:36:48.030 回答
0

首先检查是否$_POST有元素

if($_POST){
   $a = $_POST["salary"]; 
 } 
于 2013-07-07T07:47:30.063 回答