Javascript 模态不';我不想出现在login.php页面上

Javascript 模态不';我不想出现在login.php页面上,javascript,php,jquery,modal-dialog,Javascript,Php,Jquery,Modal Dialog,我在页面的开头有这个,php检查数据库中是否有密码和电子邮件,当没有显示错误时,错误应该在模式中: <?php session_start(); session_destroy(); ?> <?php session_start(); $pdo = new PDO('mysql:host=localhost;dbname=******', '*****', '******'); if(isset($_GET['l

我在页面的开头有这个,php检查数据库中是否有密码和电子邮件,当没有显示错误时,错误应该在模式中:

 <?php
    session_start();
    session_destroy();
    ?>


    <?php 
    session_start();
    $pdo = new PDO('mysql:host=localhost;dbname=******', '*****', '******');

    if(isset($_GET['login'])) {
        $email = $_POST['email'];
        $passwort = $_POST['passwort'];

        $statement = $pdo->prepare("SELECT * FROM users WHERE email = :email");
        $result = $statement->execute(array('email' => $email));
        $user = $statement->fetch();

        //Überprüfung des Passworts
        if ($user !== false && password_verify($passwort, $user['passwort'])) {
            $_SESSION['userid'] = $user['id'];
            header("Location: http://localhost/app/index.php");
        } else {

            $errorMessage = "<script type='text/javascript'>
            $(document).ready(function(){
            $('#modal1').modal('show');
            });
            </script>";
        }
    }
    ?>


在php中提交每个表单页面后重新加载。 因此,你不能显示模态。 然后填写
$errorMessage
,但不回显。 我建议您使用js来显示模态,或者比使用ajax更好 若登录失败,您必须将用户重定向到登录页面,但url中有一条消息 像这样
login.php?msg=login\u失败
使用js,你可以搜索到它

if(location.search === "msg=Login_Failed"){
           //run modal or sweet alert or anything 
       }
或 您可以将ajax用于提交表单

<script>
var username = document.getelementbyid('username').value;
var password = document.getelementbyid('password').value;
 $.post("your_php_file.php",
  {
    username: username,
    password: password
  },
  function(data){
   if(data == true){
window.location.href = "app/index.php";
}else{
//run modal or anyting here
}
  });
</script>

var username=document.getelementbyid('username')。值;
var password=document.getelementbyid('password')。值;
$.post(“您的_php_file.php”,
{
用户名:用户名,
密码:密码
},
功能(数据){
如果(数据==true){
window.location.href=“app/index.php”;
}否则{
//在这里运行模态或任何东西
}
});

看起来脚本只存在于PHP变量中,您是否真的将该变量回显到页面?嘿,我添加了echo$errorMessage;但还是不行。你到底在哪里加了回声?另外,请定义“它不工作”。嘿,谢谢你的评论,但我已经用Hosen在Jast中建议的方法解决了这个问题:
if(location.search==“msg=Login\u Failed”){//run modal或sweet alert或任何东西}
因此,如果登录表单中的密码或名称有误,我会将用户重定向到login.php?msg=login\u Failed页面,然后js脚本会跟踪浏览器url位置中是否有“?msg=login\u Failed”。如果是,则打开模式。
<script>
var username = document.getelementbyid('username').value;
var password = document.getelementbyid('password').value;
 $.post("your_php_file.php",
  {
    username: username,
    password: password
  },
  function(data){
   if(data == true){
window.location.href = "app/index.php";
}else{
//run modal or anyting here
}
  });
</script>