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");
    }
  }
}

图片转代码服务由CSDN问答提供 功能建议

添加标题后,我的模态没有显示(位置:homepage.php) 但是如果我删除标题,模态工作正常。 我怎么可能这样做? 我也尝试使用echo进行警报并发生同样的事情,所以我不知道我的代码有什么问题。 我希望有人能帮助我,谢谢你! 这是我的代码

login.php

  if(isset($ _ POST ['submit']))
 {
  $ email = $ _POST ['email']; 
 $ password = $ _POST ['password']; 
 
 $ object = new Login(); 
 $ object-&gt; getCredentials($ email,$ password)  ); 
} 
?&gt; 
 
&lt; form action =“&lt;?php echo htmlspecialchars($ _ SERVER [”PHP_SELF“]);?&gt;”  method =“POST”&gt; 
&lt; div&gt; 
&lt; label&gt;&lt; b&gt; EMAIL ADDRESS&lt; / b&gt;&lt; / label&gt; 
&lt; input type =“email”name =“email”placeholder =  “电子邮件”&gt;&lt; / input&gt; 
&lt; / div&gt; 
 
&lt; div&gt; 
&lt; label&gt;&lt; b&gt; PASSWORD&lt; / b&gt;&lt; / label&gt; 
&lt; input type =  “password”name =“password”placeholder =“Password”&gt;&lt; / input&gt; 
&lt; / div&gt; 
&lt; button type =“submit”id =“submit”name =“submit”&gt;登录&lt; / 按钮&gt; 
&lt; / form&gt; 
   
 
 

processlogin.php

 &lt;?php 
include_once'db  .php'; 
class登录
 {
公共函数getCredentials($ email,$ password)
 {
 $ email = $ email; 
 $ password = $ password; 
 
 $ object = new Db  (); 
 $ stmt = $ object-&gt; connect() - &gt; prepare(“SELECT * FROM user WHERE email =?”); 
 $ stmt-&gt; execute([$ email]); 
  $ stmtFetch = $ stmt-&gt; fetch(); 
 
 if if($ stmt-&gt; rowCount()== 1&amp;&amp; $ stmtFetch ['email'] == $ email&amp;&amp; $ stmtFetch  ['password'] == $ password)
 {
 echo“&lt; script&g  t; $('#loginsuccess')。modal('show')&lt; / script&gt;“; 
 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.

    打赏 评论
  • douzhuo1858 2018-05-19 05:19

    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.

    echo "<script>
            $('#loginsuccess').modal('show'); 
    
            setTimeout(function() {window.location.href=\"homepage.php\";}, 1000);
    
            </script>";
    
    打赏 评论

相关推荐 更多相似问题