【问题标题】:HTML in IF statement not working properlyIF 语句中的 HTML 无法正常工作
【发布时间】:2012-11-19 12:02:00
【问题描述】:

所以我有以下代码:

<?php
$users_id = $_POST['users_id'];
    if ($users_id=="1");
    {
    ?>
        <td><select name="User">
        <option></option>
        <option>John</option>
        <option>Jack</option>
        </select></td>  
<?php
    }
    if ($users_id!=="1");
    {
    ?>      
        <td align="left"> <input type="text" name="User" /><br></td></tr>
<?php
    }
    ?>

我只想显示一个表单(下拉或输入),但我总是同时得到它们并且 我无法弄清楚我的 if 语句有什么问题。 请帮忙! 感谢您的时间和精力!

【问题讨论】:

  • 为什么使用 ;在 IF 语句的末尾??? PHP是否允许这样做
  • PHP(以及许多其他受 C 影响的语言)确实允许在 IF 后使用分号,这是常见的错误来源...

标签: php html if-statement


【解决方案1】:

看起来您在不应该使用相同运算符 !== 时使用了相同的运算符,因为我看到您在另一个 if 语句中使用了相等 ==。

而且,更重要的是,您在 if 条件之后放置了分号!这将在if 条件之后创建一个空语句,之后的任何内容都将运行,无论它是否满足条件。

尝试改变

if ($users_id!=="1");

进入

if ($users_id!="1")

和

if ($users_id=="1");

到

if ($users_id=="1")

【讨论】:

    【解决方案2】:

    删除您的代码并粘贴此代码... 它肯定会工作

    <?php
    $user_id = (isset($_POST['users_id'])) ? $_POST['users_id'] : '';
    if($user_id == 1){
    ?>
    <td><select name="User">
    <option></option>
    <option>John</option>
    <option>Jack</option>
    </select></td>
    <?php
    } else {
    ?>
    <td align="left"> <input type="text" name="User" /><br></td>
    <?php
    }
    ?>
    </tr>
    

    对您的代码所做的更改 完成 post 字段的验证, 删除;从 if 条件, 使用 if-else 代替 if-if, 处理完后放置&lt;/tr&gt;,否则会返回可见错误

    【讨论】:

      【解决方案3】:

      如果 ($users_id!="1");

      试试这个。

      【讨论】:

        【解决方案4】:

        使用 if/else 块而不是两个 if()s:

        if( $users_id=="1" ) {
        
          // ... something
        
        } else {
        
          // something else
        
        }
        

        请不要使用===,除非您了解== 和=== 之间的区别。在此处查看文档:http://www.php.net/manual/en/language.operators.comparison.php

        【讨论】:

        • 我得到错误:解析错误:语法错误,意外的'else'(T_ELSE)
        【解决方案5】:

        两个变化

        1. 删除if条件后的分号

          if ($users_id!="1")

        2. 将 !== 更改为 !=

        【讨论】:

          【解决方案6】:

          从 if 行中删除分号并将第二个 if 条件更改为:

          if ($users_id!="1")
          

          或使用以下语法:

          if ($users_id=="1") {
              // select
          } else {
             // input
          }
          

          【讨论】:

          • 是的,分号是问题所在!非常感谢!
          【解决方案7】:

          删除if条件后出现的分号

          【讨论】:

            【解决方案8】:

            请检查您的代码更改

            if ($users_id!=="1");
            

            到

            if ($users_id!="1")
            

            也放弃 if();到 if() 并请阅读If else

            【讨论】:

              【解决方案9】:

              if ($users_id!=="1");//正确->if ($users_id!=="1")

              <?php
              $users_id = $_POST['users_id'];
              
                  if ($users_id==1);
                  {
              
                   echo   "<td><select name='User'>
                      <option></option>
                      <option>John</option>
                      <option>Jack</option>
                      </select></td>";  
              
                  }
                  if($users_id!=1)
                  {
              
                      echo "<td align='left'> <input type='text' name='User' /><br></td></tr>";
              
                  }
                  ?>
              

              【讨论】:

                猜你喜欢
                • 2019-07-23
                • 2015-09-27
                • 2016-04-14
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2016-10-06
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多