我正在处理一项尝试在 IE 上提交表单的任务:
1.- 当我在 page1.php 上并单击“提交”时,我会看到一个 jQuery UI 弹出对话框,然后单击“是的,我接受”,它会将我带到 page2.php,如果我单击“后退”按钮让我回到 page1.php
2.- 我还有一个名为“inProgress”的 php SESSION,当用户从 page1.php 转到 page2.php 时,它会分配一个值 1。这基本上意味着只要用户只点击一次“是的,我接受”按钮,用户就不应再显示弹出窗口(即使用户来回切换)。
问题:
a) 不知何故,当用户点击“提交”按钮(在 page1.php 上)时,弹出窗口显示并自动点击“是的,我接受”,但是即使我回去这是一件好事。不过,我更愿意自己点击“是的,我接受”按钮。
b) 主要问题是,即使我在使用 IE-11 时来回切换,我也会一直让弹出窗口显示(我只希望弹出窗口只显示一次)。
在 IE-11 上单击“是,我接受”后,如何让弹出窗口仅显示一次,或者换句话说,如何确保在 IE-11 上正确读取 session ?我考虑过将 cookie 与 javascript 一起使用,但不确定如何实现...请有人帮助我。非常感谢你!这是我的 page1.php 和 page2.php 代码
page1.php
<?php
session_start();
if ($_POST) {
$_SESSION['inProgress'] = "1";
header("Location: page2.php");
exit;
}
?>
<html>
<head>
<title>Page1</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
</head>
<body>
<form id="homeForm" name="homeForm" method = "POST">
<input type = "submit" id="btnOpenDialog" name = "submit">
<input type="text" id="inProgress" value="<?php echo $_SESSION['inProgress'] ?>"/>
</form>
<h1>This is Page 1</h1>
<div id="dialog-confirm"></div>
<script type="text/javascript">
$(document).ready(function(){
$("#btnOpenDialog").click(function(){
var inProgress = $('#inProgress').val();
if (inProgress !== "1") {
$("#dialog-confirm").dialog({
resizable: false,
modal: true,
title: "Title",
height: 250,
width: 350,
closeOnEscape: false,
buttons: {
"Yes, I accept": function () {
$('#homeForm').submit();
},
"No, thanks": function () {
$(this).dialog('close');
}
}
});
}
});
});
</script>
</body>
</html>
page2.php
<html ng-app="store">
<head>
<title>Page1</title>
</head>
<body>
<h1>This is Page 2</h1>
<div id="dialog-confirm"></div>
<script type="text/javascript">
function myfunction() {
window.location.href="page1.php";
}
</script>
<input type="button" id="btnOpenDialog" onclick = "myfunction()" value="Back" />
</body>
</html>
最佳答案
Thnink 这不是浏览器问题(我们谈论的是 session ,它是只读的服务器端,在客户端你只需要 token 来识别你的 session )。
猜想当你返回时服务器代码没有被执行(浏览器缓存了页面并且只会加载 Assets 比如图像或javascript)。
您可以使用 localStorage 改进客户端检查的行为。有一些库可以写入 localStorage,fallback 到旧浏览器的 cookies(如果您需要旧浏览器支持)像 SimpleStorage.js(在 github 中)。
使用 localStorage 会是这样的(我将使用 localStorage,没有后备库或包装库),只要单击 Yes I accept 按钮,您就可以了到 page2 存储一个标志:
$('#btnOpenDialog').on('click', function(){
localStorage.setItem('dialogAccepted', 'true'); //you can string just strings, if you ever need to store comples data use JSON.stringify(variable) so it's parsed to json
})
然后在 page1 中,检查您已经在执行服务器端( session 之类的)并添加此客户端:
if(localStorage.getItem('dialogAccepted') === 'true'){
//code to disable the dialog pop up
}
解决“自动”点击:这不是自动点击,只是对话框没有阻止表单提交。你可以这样解决:
$(document).ready(function () {
var preventedDefaultMet = false;
$("#btnOpenDialog").on('click', function (event) {
var inProgress = $('#inProgress').val();
if (inProgress !== "1") {
if (!preventedDefaultMet) {//so when the dialog was accepted we can trigger the click event and do not enter an infinite loop of dialogs
event.preventDefault();//to prevent the browser from making the POST call
}
$("#dialog-confirm").dialog({
resizable: false,
modal: true,
title: "Title",
height: 250,
width: 350,
closeOnEscape: false,
buttons: {
"Yes, I accept": function () {
preventedDefaultMet = true; // set flag
$('#homeForm').trigger('submit');//accepted! just trigger the click! yay!
},
"No, thanks": function () {
$(this).dialog('close');//trigger the click
}
}
});
}
});
});
只需混合我给您的两个答案,您就应该拥有您想要的行为。顺便说一下,不知道你是否出于某种原因执行了 $_SESSION['inProgress'] = "1";,但是你可以编写一些 javascript 并将该标志存储在浏览器内存中,就像这样:
<form id="homeForm" name="homeForm" method = "POST">
<input type = "submit" id="btnOpenDialog" name = "submit">
<input type="text" id="inProgress" value="<?php echo $_SESSION['inProgress'] ?>"/>
</form>
<script>
// when the browser reads this it will store the value PHP printed into the javascript variable
var someJavaScriptVar = <? echo $_POST ? true : false ;?>;
</script>
这样您就不必执行 $_SESSION['inProgress'] = "1"; 并且 input 将具有用户输入的值。您必须将 if (inProgress !== "1") { 更改为 if (someJavaScriptVar) {。试一试。
关于javascript - 如何在 IE-11 上正确读取 $_SESSION?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36336240/
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信
我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"
我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121