如果/ else if语句不适用于PHP表单过滤器

时间:2013-12-09 00:11:51

标签: php forms filter

我想弄清楚为什么当我提交此表单过滤器时,我的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>

1 个答案:

答案 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

之间的区别