javascript - 如何在php中的if block 中显示div

标签 javascript php

我有 index.php,在 index.php 中有一个带有 session 的登录表单。如果用户名和密码正确,它将转到 home.php。但是,如果用户名或密码不正确,我想在登录表单上方显示 div,其中包含这样的错误消息 Error Message .

我希望这个警告 div 是隐藏的,只有当用户名或密码不正确时才会显示。我尝试了其他一些解决方案,但都没有用,首先,如果我将 div 置于 else 条件下,无论条件如何,它都会出现在登录表单上方。其次,如果我用 javascript 来改变样式 display = block,它就不会出现在索引页面中。请帮我解决这个问题。谢谢。

索引.php

       <?php
          session_start();
          if(isset($_POST['login']))   
          {
            $user = $_POST['user'];
            $pass = $_POST['pass'];
            if($user == "admin" && $pass == "admin") 
            {                                      
              $_SESSION['user']=$user;
            ?>
              <script language="javascript">window.location.href='home.php' </script>
            <?php
            } else {
            //What should I do here?                
            ?>
              <script language="javascript">document.getElementById("warning").style.display = "block" </script>
            <?php
            }
          }
        ?>

        <!-- Warning that I want to show if username or password is incorrect -->
        <div id="warning" class="w3-container w3-red w3-text-white w3-card-2 w3-animate-opacity" style="position: relative;margin-top: 20px;display:none">
            <span onclick="this.parentElement.style.display='none'" class="w3-closebtn">&times;</span>
            <strong>Warning!</strong> Incorrect Username or Password!
        </div>
        <!-- Login form -->
        <form class="w3-container" action="" method="post">
        </form>

[编辑]

我最终按照 Sharky 的建议将 php 代码放在警告 div 下面解决了这个问题。在其他情况下,我仍在使用 javascript,它将更改警告 div 的样式显示以阻止。感谢您的帮助:)

最佳答案

最好的方法应该是您已经尝试过的方法:在 else 情况下使用#warning div。可能是因为你选错了 else 而没用?

<?php
      session_start();
      if (isset($_POST['login'])) {
        $user = $_POST['user'];
        $pass = $_POST['pass'];
        if($user == "admin" && $pass == "admin") {                                      
          $_SESSION['user']=$user;
          ?>
          <script type="text/javascript">window.location.href='home.php';</script>
          <?php
        } else {
          ?>
          <div id="warning" class="w3-container w3-red w3-text-white w3-card-2 w3-animate-opacity" style="position: relative;margin-top: 20px;">
          <span onclick="this.parentElement.style.display='none'" class="w3-closebtn">&times;</span>
          <strong>Warning!</strong> Incorrect Username or Password!
          </div>
          <?php
        }
      }
    ?>

    //Login form
    <form class="w3-container" action="" method="post">
    </form>

另一个技巧(与您的问题无关,但可能有帮助):您可以简单地使用 php,而不是使用 JavaScript 的重定向,这样就不再需要额外的浏览器往返:

header('Location: home.php');

这要求在调用 header() 之前不会创建“正常”输出,并且在本例中可以正常工作,但如果在它之前有一些 html 代码,则可能会失败。

关于javascript - 如何在php中的if block 中显示div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37673113/

相关文章:

javascript - 如何每 5 秒更新一次传单标记

javascript - 仅当两个元素未获得焦点时才发生 jQuery 事件

javascript - 在用户生成的可放置矩阵中将可拖动元素居中

php - 为什么我的 PHP 查询不起作用?

php - PHP Heredoc语法(<<<)在关闭Heredoc时始终在注释我的代码?

php - 获取表单数据并将其发送到浏览器中的新选项卡

javascript - 按自定义顺序对对象数组进行排序

javascript - 没有抛出错误,但我仍然无法为包含 RGB 字符串的变量分配 backgroundColor

php - 余弦相似度与汉明距离

php - 更改选项字段时新增查询数据库