如何在php中使用if语句的复选框
伙计们。请提供帮助,如果使用if语句甚至php中的switch case选中两个或多个复选框的特定组合,我想输出文本。例如,如果用户检查数学和地理,程序会说“你有资格获得野生动物管理荣誉学位”,这更像是一个小型大学咨询系统。问题是我不知道如何对单个复选框使用“if”语句如何在php中使用if语句的复选框,php,html,Php,Html,伙计们。请提供帮助,如果使用if语句甚至php中的switch case选中两个或多个复选框的特定组合,我想输出文本。例如,如果用户检查数学和地理,程序会说“你有资格获得野生动物管理荣誉学位”,这更像是一个小型大学咨询系统。问题是我不知道如何对单个复选框使用“if”语句 <!DOCTYPE HTML PUBLIC> <html> <head> <title>checkbox</title> </head>
<!DOCTYPE HTML PUBLIC>
<html>
<head>
<title>checkbox</title>
</head>
<body>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<p>
What subjects did you pass at A 'Level?<br/>
<input type="checkbox" name="subject[]" value="A" />Maths<br />
<input type="checkbox" name="subject[]" value="B" />English<br />
<input type="checkbox" name="subject[]" value="C" />Science<br />
<input type="checkbox" name="subject[]" value="D" />Religious Education<br />
<input type="checkbox" name="subject[]" value="E" />Geography
</p>
<input type="submit" name="formSubmit" value="Submit" />
</form>
<?php
$aSub = $_POST['subject'];
if(empty($aSub))
{
echo("You didn't select any subjects.");
}
else
{
$N = count($aSub);
echo("You selected $N subject(s): ");
for($i=0; $i < $N; $i++)
{
echo($aSub[$i] . " ");
}
}
?>
</body>
</html>
复选框
为了简化选项,您可以将主题合并为一个字符串并进行检查。有很多选项(60(5x4x4x2x1/2),其中包含很多if语句:-)
建议您使用此逻辑替换for循环,并从中展开:
// Array of subjects is imploded into a string
$chosen_subjects = implode("", $aSub);
if ($chosen_subjects == 'AE')
{
echo('You qualify for a Honors Degree in wildlife management');
}
PHP表单复选框示例
你很接近:-)
以下是用注释更新的PHP代码:
if (isset($_POST['subject'])) // This check is added, so you don't check an unset variable on load of page and get a warning.
{
$aSub = $_POST['subject'];
if(empty($aSub))
{
echo("You haven't selected any subjects.");
}
else
{
$N = count($aSub);
echo("You selected $N subject(s): ");
}
//array of subjects is imploded into string
$chosen_subjects = implode ("", $aSub);
// use print_r to print content of either string or array/object
// print_r($aSub);
// print_r($chosen_subjects);
// This is the result of the above 2 print_r
// Array
// (
// [0] => Agriculture
// [1] => Biology
// )
// AgricultureBiology
// if ($chosen_subjects == 'Biology' && 'Agriculture' )
/**
* If you wanted to test for those two variables "Biology" and "Argriculture" then you have to do like this:
* if ($aSub[0] == 'Agriculture' && $aSub[1] == 'Biology')
* By using implode, we are converting the array of options into a string.
*/
if ($chosen_subjects == 'AgricultureBiology' )
{
echo "You qualify for a Bachelor of Agricultural Sciences Honors Degree in Animal Science and Rangeland Management";
}
}
请注意,这种方法可能不是最简单的。您现在有14个选项,即43589145600个可能的组合(14!/2),这太多了:-)
一个建议。
你有多少学士学位?假设你有5个。如果您显示了这些内容,然后用户可以单击该框,该框随后展开,然后显示需要哪些科目以及哪些分数,则表示和理解起来会简单得多。可能与“谢谢”重复,我使用了该逻辑,我不断收到一个错误,上面写着“警告:内爆():在C:\Users\Otis Mabikwa\Desktop\test.php的第52行“中传递的参数无效,在尝试使用if语句检查两个条件后,if语句忽略了第二个条件,只检查了第一个条件($selected_subjects=='Biology'&'Agriculture')。只有在Biology为true时才执行echo,即使农业是假的
if (isset($_POST['subject'])) // This check is added, so you don't check an unset variable on load of page and get a warning.
{
$aSub = $_POST['subject'];
if(empty($aSub))
{
echo("You haven't selected any subjects.");
}
else
{
$N = count($aSub);
echo("You selected $N subject(s): ");
}
//array of subjects is imploded into string
$chosen_subjects = implode ("", $aSub);
// use print_r to print content of either string or array/object
// print_r($aSub);
// print_r($chosen_subjects);
// This is the result of the above 2 print_r
// Array
// (
// [0] => Agriculture
// [1] => Biology
// )
// AgricultureBiology
// if ($chosen_subjects == 'Biology' && 'Agriculture' )
/**
* If you wanted to test for those two variables "Biology" and "Argriculture" then you have to do like this:
* if ($aSub[0] == 'Agriculture' && $aSub[1] == 'Biology')
* By using implode, we are converting the array of options into a string.
*/
if ($chosen_subjects == 'AgricultureBiology' )
{
echo "You qualify for a Bachelor of Agricultural Sciences Honors Degree in Animal Science and Rangeland Management";
}
}