【问题标题】:Hide Form Div Using PHP if Statment使用 PHP if 语句隐藏表单 Div
【发布时间】:2021-05-25 18:48:47
【问题描述】:

尝试在表单内成功登录后隐藏登录表单,以便用户登录后无法看到该表单,但我仍然需要在成功时显示成功消息登录已经发生。 我对表单和登录名进行了排序,成功登录后显示成功消息,但成功登录后我无法隐藏表单。 我有一个使用 isset 设置的 if 语句,但据我所知,我不知道在 if 语句中需要做什么才能隐藏表单。

这是我的登录页面的 PHP 代码 -

        <?php
    require('includes/application_top.php');
    $page_title='Login!';
    if (isset($_POST['action'])) {
        $username_input = $_POST['username'];
        $password_input = $_POST['password'];
        $login_ok = login($username_input, $password_input);
    }
    require('includes/site_header.php');
    ?>
    <style>
    <?php
    require('css/login.css');
    ?>
    </style>
    <br>
    <br>
    <br>
    <?php
            if (isset($login_ok)) {
                ?>
                <div class="row">
                    <div class="col-sm-12">
                        <?php
                        if ( ! $login_ok) {
                            ?>
                            <div class="alert alert-danger">
                                <p>Your credentials were invalid</p>
                            </div>
                            <?php
                        } else {
                            ?>
                            <div class="alert alert-success">
                                <p>Login successful</p>
                            </div>
                            <?php
                        }
                        ?>
                    </div>
                </div>
                <?php
            }
            ?>
    <!-- This is the if statement I mention -->
    <?php 
         if !isset($login_okay){
            ?>
            <div class="wrapper dafswInDown" hidden>
          }
    ?>


    <div class="wrapper fadeInDown">
      <div id="formContent">
        <!-- Icon -->
        <div class="fadeIn first">
          <br>
          <img src="images/Head1.jpg" style="height: 40%; width: 60%;" id="icon" alt="User Icon" />
          <br>
          <h3> Login! </h3>
        </div>
        <br>
        <!-- Login Form -->
        <form id="login_form" action="login_page.php" method="post">
          <input type="text" class="fadeIn second" id="username" name="username" placeholder="Username" value="<?= isset($_POST['username']) ? htmlspecialchars($_POST['username']) : '' ?>" maxlength="100" />
          <input type="password" class="form-control" id="password" name="password" placeholder="Password" value="" />
          <input type="submit" class="fadeIn fourth" name="action" value="Login"/>
        </form>

        <!-- Remind Passowrd -->
        <div id="formFooter">
          <a class="underlineHover" href="#">Forgot Password?</a>
        </div>


      </div>
    </div>


    <?php
    require('includes/application_bottom.php');
    require('includes/site_footer.php');
    ?>

【问题讨论】:

  • @bromber 这可能是错误的,就像我在被卡住之前所做的那样
  • @brombeer 是的,应该有括号我现在已经改变了
  • 你的方法有什么不适用的?要么像你一样使用开头的&lt;div&gt;(记得在你的表单之后使用相同的if关闭它)或者将你的完整表单放在if中,这样它就不会被渲染。
  • 页面没有加载我现在拥有的内容,会将整个 div 放入 if 语句中
  • 还要注意命名东西,你在上面使用$login_ok,但在if中使用$login_okay

标签: php html css forms


【解决方案1】:

有许多不同的方法可以解决这个问题,但我认为最好的方法是使用会话变量。让您的登录函数创建一个会话变量,该变量将跟随整个网站的登录用户,然后只需检查该值并根据该值显示或隐藏表单。这是一个简化的例子:

<?php function login($username_input=false, $password_input=false){
  // Do all your verification here
  // If logged in then:
  session_start();
  $_SESSION["user"] = true;
}

// Then on the form side just do this:
if(!$_SESSION["user"]){ ?> 
  FORM HERE
<?php } ?>

【讨论】:

  • 没有会话我该怎么做?
  • 您需要会话。当用户移动到另一个页面时,您如何知道用户已登录?
  • 因为这是一个大学项目,我很快就会参加那个部分,有必要开会吗?
  • 另外,您可以设置某种全局变量,但我不推荐它。
  • 我在会话中尝试了你的方式,但它没有用,我应该把东西放在代码中的什么地方,因为我把我的表格放在你说的地方,但它没有用
猜你喜欢
  • 2020-04-07
  • 1970-01-01
  • 1970-01-01
  • 2017-02-05
  • 1970-01-01
  • 2014-10-16
  • 1970-01-01
  • 1970-01-01
  • 2021-10-04
相关资源
最近更新 更多