我想弄清楚为什么当我提交此表单过滤器时,我的if / else if语句不起作用。的print_r($后);返回正确的过滤器值,但是$ printmain只显示'rehab',无论选择哪个过滤器选项 - 这很奇怪,因为$ post变量显然正在改变。所以我无法弄清楚为什么它没有通过if / else if语句?
if(isset($_POST['filter'])) {
$post = $_POST['filter'];
print_r($post);
if ($post = 'teamrehab') {$printmain = 'rehab';}
else if ($post = 'heights') {$printmain = 'heights';}
else if ($post = '1225') {$printmain = '1225';}
}
<html>
<form method="post" id="filter" action="<?= $_SERVER['PHP_SELF'];?>">
<select name="filter" onchange="document.getElementById('filter').submit();">
<option value="choose">Choose Client</option>
<option value="teamrehab">teamrehab</option>
<option value="heights">heights</option>
<option value="1225">1225 Old Town</option>
</select>
</form>
<?php if(!empty($_SESSION['username'])){ echo $printmain;}
else echo "Please login with your Twitter account.";?>
</html>
答案 0 :(得分:3)
您在if语句中使用的是=
而不是==
。
更改以下内容:
if ($post = 'teamrehab') {$printmain = 'rehab';}
else if ($post = 'heights') {$printmain = 'heights';}
else if ($post = '1225') {$printmain = '1225';}
为:
if ($post == 'teamrehab') {$printmain = 'rehab';}
else if ($post == 'heights') {$printmain = 'heights';}
else if ($post == '1225') {$printmain = '1225';}
您需要了解=
assignment operator和==
comparison operator