我很难弄清楚如何获取用于switch语句的单选按钮的值。基本上,当用户选择其中一个单选按钮时,我希望执行该单选按钮的操作。不确定我是否正确设置。我正在自己学习PHP,并且不知道这是否是正确的方法。下面是HTML和PHP。
<input class="radio" type="radio" name="calculate" value="average" checked="checked">Average<br />
<input class="radio" type="radio" name="calculate" value="total">Total<br />
<input class="radio" type="radio" name="calculate" value="both">Both<br />
这是PHP
$calculate_type = $_POST['calculate'];
switch ($calculate_type) {
case '$calculate_type == "average"':
$score_average = $score_total / count($scores);
break;
case '$calculate_type == "total"':
$score_total = $scores[0] + $scores[1] + $scores[2];
break;
case '$calculate_type == "both"':
$score_average = $score_total / count($scores);
$score_total = $scores[0] + $scores[1] + $scores[2];
break;
}
答案 0 :(得分:2)
你到底在哪里学会写出switch
这样的陈述?
switch($calculate_type) {
case "average":
// do something
break;
case "total":
// do something else
break;
case "both":
// do something completely different
break;
default: die("Invalid type");
}
然后,在这种情况下,它会更好,如下所示:
HTML:
<input class="radio" type="radio" name="calculate" value="1" checked="checked">Average<br />
<input class="radio" type="radio" name="calculate" value="2">Total<br />
<input class="radio" type="radio" name="calculate" value="3">Both<br />
PHP:
if( $_POST['calculate'] & 1) $score_average = $score_total / count($scores);
if( $_POST['calculate'] & 2) $score_total = $scores[0]+$scores[1]+$scores[2];