【问题标题】:If/else if statements not work with PHP form filterif/else if 语句不适用于 PHP 表单过滤器
【发布时间】:2013-12-25 23:49:20
【问题描述】:

我试图弄清楚为什么当我提交此表单过滤器时,我的 if/else if 语句不起作用。 print_r($post);返回正确的过滤器值,但是无论选择什么过滤器选项,$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>

【问题讨论】:

  • 您的比较运算符 = 正在分配一个不比较 == 的值

标签: php forms filter


【解决方案1】:

您在 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 之间的区别

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多