![](/img/trans.png)
[英]How can I capture php session variables in javascript without page refresh?
[英]How to refresh a separate page to get PHP session variables?
在开始之前,我需要您知道我的方法不是最佳方法。 我是JavaScript和php的初学者,而且我确信可以通过更好,更简单的方法来实现自己想要的目标。
我认为我的问题在于会话变量和打印页面。 我有一个页面,用户可以从下拉列表中创建表。 我需要在三个不同的页面上以三种不同的格式打印表格。 我使用“ frames [“ iframe] .print”功能链接了三个要打印的页面。
这是我的相框:
<iframe src="customer_detail.php" style="display:none;"name="framed"></iframe>
<iframe src="advisor_copy.php" style="display:none;" name="framez"></iframe>
<iframe src="customer_copy.php" style="display:none;" name="frame"></iframe>
和我的按钮:
<span id = "cCopy"><button class = "button" onclick= "frames['frame'].print();"/> Customer Copy</button></span>
<span id = "aCopy"><button class = "button" onclick="frames['framez'].print();" value="Advisor_Copy">Advisor Copy</button> </span>
<span id = "cCopy"><button class ="button" onclick= "frames['framed'].print();" value="customer_copy">Customer Detail </button></span>
据我了解,页面需要刷新以更新会话变量。 我有效的黑客解决方案是每1秒刷新我正在打印的3页(customer_detail,customer_copy和advisor_copy)。 我更希望能够单击三个按钮之一时刷新正在打印的页面,以便可以更新在这些页面上调用的会话变量。
我尝试使用onclick事件刷新iframe,但它并不能完全满足我的需要。 我到处寻找各种解决方案,但没有一个真正起作用。 如果我需要重组代码,则可以。 我认为我真正应该做的是通过AJAX调用将所需的变量传递给其他页面。 我想知道是否有一种解决方案可以使我的代码运行而不必或多或少从头开始。
function refreshPage() {
$("#widgetbox").fadeOut(750, function() {
<!-- empty the current content and then fetch new data -->
$("#widgetbox").empty
<!-- load the getPage.php file which will replace all the
$("#widgetbox").load("getPage.php", function() {
$("#widgetbox").fadeIn();
});
});
};
<!-- initiate the function above -->
$(function(){
refreshPage();
<!-- set the interval for refreshing, default set to 1.0 seconds -->
var int = setInterval("refreshPage()", 1000);
});
研究此http://websitetutorials.net/jquery/rotate-product-listings-php-and-jquery并对其进行修改
好。 这是一个丑陋的解决方法,没有全部重写。
<span id = "cCopy"><button class = "button"><a href="customer_detail.php" target="framed">Customer Copy</a></button> </span>
<span id = "aCopy"><button class = "button" value="Advisor_Copy"><a href="advisor_copy.php" target="framez">Advisor Copy</a></button> </span>
<span id = "cCopy"><button class ="button" value="customer_copy"><a href="customer_copy.php" target="frame">Customer Detail</a> </button></span>
<br>
<iframe name="framed"></iframe>
<iframe name="framez"></iframe>
<iframe name="frame"></iframe>
UPDATE
...以及在单个PHP页面上的body标签上... <body onload="window.print()">
更新2
如果要删除下划线按钮文本,请添加
style="text-decoration:none;"
在每个<a
标签上
更新3 (由于浏览器的安全锁,有必要进行一些测试)
您的index.html:
<span id = "cCopy"><button onclick="printIframe('framed')" class = "button">Customer Copy</button></span>
<span id = "aCopy"><button onclick="printIframe('framez')" class = "button" value="Advisor_Copy">
Advisor Copy</button></span>
<span id = "cCopy"><button onclick="printIframe('frame')" class ="button" value="customer_copy">Customer Detail</button></span>
<br>
<div id='frames'>
</div>
<script>
var iframes = {}; //create a 'container' for iframe
iframes['framed'] = 'customer_detail.php'; // your link
iframes['framez'] = 'advisor_copy.php'; // your link
iframes['frame'] = 'customer_copy.php'; // your link
// if you want to add another iframe create a new line with iframes['myIframeName'] = 'MyIframeUrlLink'; // your link
for (var k in iframes){
createIframe(k, iframes[k]);
}
function printIframe(id) //this call a javascript print function inside your PHP - need for security reason
{
var iframe = document.frames ? document.frames[id] : document.getElementById(id);
var ifWin = iframe.contentWindow || iframe;
iframe.focus();
ifWin.printPage();
return false;
}
//this create iframes
function createIframe(name, src) {
var i = document.createElement("iframe");
i.name = name;
i.id = name;
i.src = src;
i.style= "display: none";
document.getElementById("frames").appendChild(i);
};
</script>
在您的PHP Pages中, 删除我的onload="window.print()"
并在body
标记的末尾</body>
之前添加:
<script>
function printPage() { print(); }
</script>
像这样:
</body>
<script>
function printPage() { print(); }
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.