dongque8332 2018-05-19 00:28
浏览 67
已采纳

在php中登录后,模态没有显示出来

My modal is not showing up after I add header(location: homepage.php) but modal is working fine if I remove the header. How can I possibly do it? I've also tried using echo for alert and same thing happens, so I don't know what is wrong with my code. I hope someone can help me thank you!. Here is my code

login.php

if(isset($_POST['submit']))
{
    $email = $_POST['email'];
    $password = $_POST['password'];

    $object = new Login();
    $object->getCredentials($email, $password);
}
?>

<form  action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="POST">
<div>
 <label><b>EMAIL ADDRESS</b></label>
 <input type="email" name="email" placeholder="Email"></input>
</div>

<div>
 <label><b>PASSWORD</b></label>
 <input type="password" name="password" placeholder="Password"></input>
</div>
<button type="submit" id="submit" name="submit">Login</button>
</form>

processlogin.php

<?php
include_once 'db.php';
class Login
{
  public function getCredentials($email, $password)
  {     
    $email = $email;
    $password = $password;

    $object = new Db();
    $stmt = $object->connect()->prepare("SELECT * FROM user WHERE email=?");
    $stmt->execute([$email]);
    $stmtFetch = $stmt->fetch();

    if($stmt->rowCount()==1 && $stmtFetch['email'] == $email && $stmtFetch['password'] == $password)
    {
      echo "<script>$('#loginsuccess').modal('show')</script>";
      header("location: homepage.php");
    }
  }
}
  • 写回答

2条回答 默认 最新

  • duanmeng1862 2018-05-19 00:56
    关注

    I found something that might interest you here:
    Interview Question: Can we have an echo before header?

    The problem is that we cannot send the header after we start sending the output, and if you send the header before the echo, the echo will not be executed.

    Try this:
    Solution 1: (from the link above).

    ob_start();
    echo "<script>$('#loginsuccess').modal('show')</script>";
    header("location: homepage.php");
    ob_end_flush();
    

    Solution 2: Use a javascript redirection instead of header function.

    echo "<script>
            $('#loginsuccess').modal('show');
            window.location.replace('http://fullpath-homepage.php');
          </script>";
    

    Additional note:
    You can use also:

    window.location.href="http://example.com";
    window.location.assign("http://example.com");
    

    The replace method navigates to the URL without adding a new record to the history.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”