简体   繁体   English

登录php后未显示模态

[英]Modal not showing up after login in php

My modal is not showing up after I add header(location: homepage.php) but modal is working fine if I remove the header . 添加header(location: homepage.php)后,我的模态没有显示,但是如果删除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. 我也尝试过使用echo来发出警报,并且发生了同样的事情,所以我不知道我的代码有什么问题。 I hope someone can help me thank you!. 希望有人能帮我谢谢! Here is my code 这是我的代码

login.php 的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 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");
    }
  }
}

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. 问题在于,在开始发送输出之后我们无法发送标头,并且如果您在echo之前发送标头,将不会执行echo

Try this: 尝试这个:
Solution 1: (from the link above). 解决方案1 ​​:(从上面的链接)。

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. 解决方案2:使用JavaScript重定向而不是标头函数。

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. replace方法可导航到URL,而无需在历史记录中添加新记录。

UPDATE: FOUND THE ANSWER 更新:发现答案

I use Yeti82's answer and this is what I did to make the delay to show modal of success and then directing to the next page. 我使用Yeti82的答案,这是我做些延迟以显示成功的方式,然后转到下一页。

echo "<script>
        $('#loginsuccess').modal('show'); 

        setTimeout(function() {window.location.href=\"homepage.php\";}, 1000);

        </script>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM