“打开模式窗口”;使用;PHP
我正在用PHP创建一个登录系统,我正在努力使它变得更好 当您注销时,会被重定向回index.php。像这样:“打开模式窗口”;使用;PHP,php,jquery,modal-dialog,Php,Jquery,Modal Dialog,我正在用PHP创建一个登录系统,我正在努力使它变得更好 当您注销时,会被重定向回index.php。像这样: header("loaction: index.php?logout=true") 这使url看起来像www.mysite.com/index.php?logout=true。然后我使用以下代码: if(isset($_GET['logout'])) { $logoutvalue = $_GET['logout']; if($logoutvalue = "true") { $not
header("loaction: index.php?logout=true")
这使url看起来像www.mysite.com/index.php?logout=true。然后我使用以下代码:
if(isset($_GET['logout'])) {
$logoutvalue = $_GET['logout'];
if($logoutvalue = "true") {
$notification = "You've been logged out!";
}
获取从URL注销的值并对其进行处理
我有一个小弹出窗口,显示通知变量的值。在本例中,它是“You's logged out!”,我的问题是如何在页面加载以及url等于/index.php?logout=true时显示模式窗口
非常感谢所有评论、答案和建议
谢谢!
-Ryan您是否正在寻找类似于:
<script>
<?php if(isset($_GET['logout']) && $_GET['logout'] === 'true'){
echo 'alert("You\'ve been logged out!");'
}
?>
</script>
或者您甚至可以在if语句中编写脚本:
<?php
if(isset($_GET['logout']) && $_GET['logout'] === 'true'){ ?>
<script type="text/javascript>
window.open(yourhtml);
</script>
</php }
else {
// may be some other script
}
?>
请下载模态JS库
并使用该条件
if(isset($_GET['logout'])) {
$logoutvalue = $_GET['logout'];
if($logoutvalue = "true") {
code to open the Modal as in the JS library.....
}
首先,
不能直接“使用PHP打开模式窗口”。
只有通过交换变量(通过JSON或XML),或者将PHP条件嵌入到标记中,才能做到这一点
PHP和JavaScript是独立的
我的问题是,当页面打开时,如何让模式窗口显示
当url等于/index.php?logout=true
有两种方法可以实现这一点
首先:充分利用将PHP条件嵌入到标记中
Second:在某处隐藏输入,如(
),然后通过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>
window.onload=函数(){
if(document.getElementById('showmodel')){
警报('Box');//替换为您自己的处理程序
}
}
我知道这是一篇老文章,但为了帮助将来有类似任务的人,我想下面的内容可能有助于进入正确的方向(我自己已经测试了下面的代码,所以请进行调整)
使用该模式完成此操作的最佳方法。
index.php
1。在HTML中为模式内容创建元素
<div id=modal></div>
#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;
}
'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.')
$(()=>{
$('#modal').notify(1500) // 1500 is the interval in milliseconds
})
<div id=modal>
<?=
@$_SESSION["notify"]; // if this variable is not set, nothing will happen
unset($_SESSION["notify"]);
?>
</div>
<a href=parse.php?logout>Logout</a>
if (isset($_GET["logout"])) {
$_SESSION["notify"] = "You have been logged out.";
header("Location: index.php");
}
3。定义如何使用jQuery显示和消失模式框的函数
<div id=modal></div>
#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;
}
'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.')
$(()=>{
$('#modal').notify(1500) // 1500 is the interval in milliseconds
})
<div id=modal>
<?=
@$_SESSION["notify"]; // if this variable is not set, nothing will happen
unset($_SESSION["notify"]);
?>
</div>
<a href=parse.php?logout>Logout</a>
if (isset($_GET["logout"])) {
$_SESSION["notify"] = "You have been logged out.";
header("Location: index.php");
}
5。将PHP发送的通知插入#modal
元素中
<div id=modal></div>
#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;
}
'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.')
$(()=>{
$('#modal').notify(1500) // 1500 is the interval in milliseconds
})
<div id=modal>
<?=
@$_SESSION["notify"]; // if this variable is not set, nothing will happen
unset($_SESSION["notify"]);
?>
</div>
<a href=parse.php?logout>Logout</a>
if (isset($_GET["logout"])) {
$_SESSION["notify"] = "You have been logged out.";
header("Location: index.php");
}
parse.php
7。在$\u会话
变量中使用PHP返回值
<div id=modal></div>
#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;
}
'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.')
$(()=>{
$('#modal').notify(1500) // 1500 is the interval in milliseconds
})
<div id=modal>
<?=
@$_SESSION["notify"]; // if this variable is not set, nothing will happen
unset($_SESSION["notify"]);
?>
</div>
<a href=parse.php?logout>Logout</a>
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”错误)
当你说“一个小弹出窗口”时,我假设你指的是一个引导型模式
if(isset($_GET['logout'])) {
$logoutvalue = $_GET['logout'];
if($logoutvalue = "true") {
$notification = "You've been logged out!";
}
}
我所做的是将PHP代码块移动到.PHP文件的末尾,在jQuery(etc)之后,并包含一个include,因此看起来是这样的(假设您的modal的名称是id=“logoutModal”):
logout.php
if(isset($_GET['logout'])) {
$logoutvalue = $_GET['logout'];
if($logoutvalue = "true") {
include("logout-modal.php");
}
}
<?php ?>
<script>
$('#logoutModal').modal('show');
</script>
<?php ?>
注销modal.php
if(isset($_GET['logout'])) {
$logoutvalue = $_GET['logout'];
if($logoutvalue = "true") {
include("logout-modal.php");
}
}
<?php ?>
<script>
$('#logoutModal').modal('show');
</script>
<?php ?>
$('#logoutModal').modal('show');
不是100%确定这能回答你的问题,但这对我来说很有效。希望这能有所帮助。标题位置应该使用完整的URI。它只在localhost上,所以一旦激活就会改变。您可以基于$\u GET值编写javascript。。这和这里的问题不一样吗:@BharathParlapalli我不认为这是相似的。我的要简单得多。我只需要以某种方式使用PHP打开一个模式窗口。一定有办法做到这一点!哈哈。我想到了JavaScript。我真的不知道该怎么做。。因为我不知道,我甚至不知道这是否有效。不完全是。我不想要JavaScript警报框。我想显示模式框。因此,如果我要单击html中的链接,那么代码将是
,如果这有帮助的话。模态窗口看起来更好PDo您知道如何创建一个在某个JS事件中出现/消失的模式框吗?您还必须获得一个UI和CSS才能显示它。@类只是一个问题(我不知道),它不应该是$(“#dialog”).modal()代码>。编辑也不起作用。我不知道这是否有帮助,但这就是我得到模态插件的地方@RyanfizGerald我给了你一个jQueryUI示例,它是.dialog()代码>你得到的站点也能正常工作,你只需要根据你想要的样子对其进行相应的修改。这与@Class的建议有什么不同?只是想知道为什么负面评价。