我正在用PHP创建一个登录系统,我试图让它更好一些。
当您注销时,您将被重定向回index.php。像这样:
header("loaction: index.php?logout=true")
这使得网址看起来像www.mysite.com/index.php?logout=true。然后我使用以下代码:
if(isset($_GET['logout'])) {
$logoutvalue = $_GET['logout'];
if($logoutvalue = "true") {
$notification = "You've been logged out!";
}
从URL获取logout的值并对其执行某些操作。
我有一个小的弹出窗口,它将显示通知变量的值。在这种情况下,它是“你已经退出了!”我的问题是如何在页面加载时以及当url等于/index.php?logout=true时显示模态窗口?
所有评论,答案和建议都非常感谢!
谢谢! - 瑞恩
首先,
您不能直接“使用PHP打开模态窗口”。您只能通过交换变量(通过JSON或XML)或将PHP条件直接嵌入到标记中来实现此目的。
PHP和JavaScript是独立的。
我的问题是如何在页面加载时以及当url等于/index.php?logout=true时显示模态窗口?
有两种方法可以实现这一目标。 第一:充分利用将PHP条件嵌入到标记中。
第二:有一些隐藏的输入,比如(<input type="hidden" id="showModal" />
),然后通过JavaScript本身检查它是否存在。
例如:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
window.onload = function(){
if ( document.getElementById('showModal') ){
alert('Box'); //replace with your own handler
}
}
</script>
</head>
<body>
<?php if ( isset($_GET['logout']) && $_GET['logout'] === 'true' ): ?>
<input type="hidden" id="showModal" />
<?php endif;?>
</body>
</html>
你在寻找类似的东西:
<script>
<?php if(isset($_GET['logout']) && $_GET['logout'] === 'true'){
echo 'alert("You\'ve been logged out!");'
}
?>
</script>
编辑:我相信你正在为模态窗口寻找这样的东西(使用jQuery)
<?php if(isset($_GET['logout']) && $_GET['logout'] === 'true'){
$message = "You've been logged out!";
?>
<script>
$(function() {
$("#dialog").dialog();//if you want you can have a timeout to hide the window after x seconds
});
</script>
<div id="dialog" title="Basic dialog">
<p><?php echo $message;?></p>
</div>
<?php } ?>
我知道这是一个老帖子,但是为了帮助将来有类似任务的人,我想以下可能有助于进入正确的方向(我自己测试了下面的代码,所以请进行调整)。
<?php if(isset($_GET['logout']) && $_GET['logout'] === 'true'){
$message = "You've been logged out!";
?>
<script>
$(function() {
$('#myModal').modal('show');
});
</script>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Edit Data</h4>
</div>
<div class="modal-body">
<div class="fetched-data"><?php $message ?></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php } ?>
使用PRG模式完成此操作的最佳方法。
1.在HTML中为模态内容创建元素
<div id=modal></div>
2.设计你的#modal
元素
#modal {
position: fixed;
display: none; /*important*/
color: white;
text-align: center;
z-index: 1000;
width: 100%;
height: 80px;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
background: crimson;
line-height: 2;
}
3.使用jQuery定义函数如何显示和消失模态框
'use strict'
window.jQuery ?
$.fn.notify = function(interval) {
let el = $(this),
text = el.text(),
modal =()=> { // this will fire after the interval has expired
el.fadeOut(),
clearInterval(timer)
},
timer = setInterval(modal,interval)
!text.match(/\S+/) || el.text('') // this will fire when modal box contains text
.append(`<h3>${text}</h3>`)
.show()
}
: alert('jQuery required.')
4.将.notify()
附加到jQuery选择器
$(()=>{
$('#modal').notify(1500) // 1500 is the interval in milliseconds
})
5.将从PHP发送的通知插入#modal
元素中
<div id=modal>
<?=
@$_SESSION["notify"]; // if this variable is not set, nothing will happen
unset($_SESSION["notify"]);
?>
</div>
6.执行简单的$_GET
请求
<a href=parse.php?logout>Logout</a>
7.使用PHP在$_SESSION
变量中返回一个值
if (isset($_GET["logout"])) {
$_SESSION["notify"] = "You have been logged out.";
header("Location: index.php");
}
这是一种很好的做法。将注销请求发送到PHP后,它将重定向到索引页面,其结果为会话变量。 PRG模式并不总是需要POST请求。此示例发送GET请求,该请求可用于注销。请注意,您必须将session_start();
放在文件的顶部。
好的,所以你现有的代码就像这个片段一样,你遇到的问题是你想要显示由/用PHP触发的jQuery / Bootstrap模式,但是jQuery / JS还没有加载(所以你会得到的)一个“$ undefined”错误)。
当你说“一个小弹出窗口”我假设你的意思是一个bootstrap类型模式。
if(isset($_GET['logout'])) {
$logoutvalue = $_GET['logout'];
if($logoutvalue = "true") {
$notification = "You've been logged out!";
}
}
我所做的是将PHP代码块移动到.php文件的末尾,在jQuery(等)之后,并且有一个include,所以它看起来像这样(假设你的模态的名称是id =“logoutModal”):
logout.php
if(isset($_GET['logout'])) {
$logoutvalue = $_GET['logout'];
if($logoutvalue = "true") {
include("logout-modal.php");
}
}
注销,modal.php
<?php ?>
<script>
$('#logoutModal').modal('show');
</script>
<?php ?>
不是100%肯定这回答了你的问题,但这对我有用。希望这可以帮助。
或者您甚至可以在if语句中编写脚本:
<?php
if(isset($_GET['logout']) && $_GET['logout'] === 'true'){ ?>
<script type="text/javascript>
window.open(yourhtml);
</script>
</php }
else {
// may be some other script
}
?>
请下载Modal JS库http://simplemodal.plasm.it
并使用条件
if(isset($_GET['logout'])) {
$logoutvalue = $_GET['logout'];
if($logoutvalue = "true") {
code to open the Modal as in the JS library.....
}