[英]jQuery call modal from PHP script
我正在創建一個登錄腳本,用於檢查用戶是否存在於表中。
如果用戶不存在,我想使用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
我假設您正在使用Bootstrap打開模式窗口,因為您使用的是.modal('show')
。 確保在HTML <head></head>
包含jquery以及bootstrap js文件。
如果模式窗口位於對$('#errLoginModal').modal('show');
的調用之下 ,您需要使用jQuery .ready
事件,該事件將在DOM完全加載后觸發docs 。
因此,您可能應該看起來像這樣:
else {
echo ("<script language='javascript'>
$( document ).ready(function() {
$('#errLoginModal').modal('show');
});
</script>");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.