javascript - 从 PHP 脚本中调用 jQuery 模态

标签 javascript php jquery modal-dialog

我正在创建一个登录脚本来检查用户是否存在于表中。

如果用户不存在,我想使用 jquery 打开一个模式窗口,让用户知道出现了错误。

 <?php
   include("../include/database.php");
   if(isset($_GET['loginSubmit']))   // form submit button
   {
 // form username and password values
     $username = strip_tags(mysqli_real_escape_string($dbc, trim($_GET['username'])));
     $password = strip_tags(mysqli_real_escape_string($dbc, trim(md5($_GET['password']))));

 // set query
     $select = "SELECT username, fullname, userlevel, `password` FROM users WHERE username = '".$username."'"; 
 // run query
     $query = mysqli_query($dbc, $select);
 // get the results
     $row = mysqli_fetch_array($query);
 // set the results to variables to be used in sessions
     $dbusername = htmlentities(stripslashes($row['username']));    
     $dbfullname = htmlentities(stripslashes($row['fullname']));
     $dbuserlevel = htmlentities(stripslashes($row['userlevel']));
     $dbpassword = htmlentities(stripslashes($row['password']));

 // check if the database password matches the user password
     if($dbpassword == $password)
     {
 // if yes, set the sessions and direct to main page
       $_SESSION['username'] = $username;
       $_SESSION['fullname'] = $dbfullname;
       $_SESSION['userlevel'] = $dbuserlevel;
       header("Location:main.html"); 
     }
     else
     {
 // if no match, this is where I want the javascript to show the modal
       echo ("<script language='javascript'>
              $('#errLoginModal').modal('show');  // this is jquery and I know it's incorrect
              window.location.href='index.php'
              </script>");
     }  
 ?>

我仍在努力改进 JavaScript、jQuery 和 PHP。通过 Stack Overflow,我变得越来越好。

我希望 PHP 脚本在用户名/密码检查失败时使用 JavaScript/jQuery 来触发模式窗口。我什至不确定这是否是最好的方法。

我知道上面的代码不正确。我的意思是它不显示模式窗口。

另一方面,我可以添加一个警报,它会像这样触发警报:

 else
 {
   echo ("<script language='javascript'>
          alert(test);
          window.location.href='index.php'
          </script>");
 }

这有效。我只是希望它能够显示模式。

最佳答案

您可以尝试包含js文件。

 }else{

   echo `<script src="you_file.js"></script>`
 }

    //you_file.js

(function ($){
    $(document).ready(function() {
        alert(`test`);
        $('#errLoginModal').modal('show');  
        window.location.href='index.php';
    })
})

当然在此之前包括jquery

关于javascript - 从 PHP 脚本中调用 jQuery 模态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29473519/

相关文章:

javascript - 如何在获得焦点时使用 css 使 Xul 按钮变大?

php - 如何在 CodeIgniter 中有条件地加载配置文件?

jQuery "on", "hover"在鼠标移出时执行

javascript - Jquery 图像大小操作

javascript - 在表格行中显示图像

asp.net - 如何捕获提交表单响应 "different domain"?

javascript - 如何在下面提供的 JS fiddle 圆形进度条上获得箭头形状

php - 关系数据库表的 ER 图

javascript - Backbone.js 设置 View 属性

php - Google 缓存显示的 Url 与我的网站 Url 不同