[英]Refresh page after button was clicked and location changed
我有一个按钮,可通过使用javascript将页面的位置更改为我的php脚本来生成Word文档,该脚本将通过更改位置在同一页面上输出文件:
header('Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment; filename="' .$templateName . '.docx"');
readfile( $tempFileName);
无论如何,当我单击按钮时,php脚本更改将更改一行数据,其中包含按钮的页面所在的行。 一旦按钮被点击,它只是生成的Word文档,但不刷新页面,这样,用户会认为按钮已经生成的Word文档,但不改变的记录,只刷新页面后,只有效应将用户注意按钮的区别。
我按钮的HTML是这样的:
<input type="button" value="Generate Final Demand" onClick="self.location='DOCXGenerateDocument.php?templ=finaldemand&invoiceid=<?=$_GET['id']?>'">
有关于文档返回后如何完成刷新页面任务的想法吗?
一种解决方案是使用setTimeout()
并猜测何时应返回页面,然后在超时触发时执行document.location.reload(true)
。
它不是很漂亮,但是应该可以完成工作。
代替执行self.location
,使用window.location
例如onClick="window.location='DOCXGenerateDocument.php?templ=finaldemand&invoiceid=<?=$_GET['id']?>'"
让我知道这个是否奏效。
有关更多参考,您可以在MDN上查看此链接 。
一个相对简单的方法是利用PHP中的会话锁定。 让我们假设您的DOCX生成脚本使用会话,并且直到脚本结束才释放会话。 现在,如果浏览器尝试加载另一个也使用会话的PHP脚本,则第二个脚本将停顿,直到第一个脚本完成。 使用“ location.reload()”将第二个脚本变成动态加载的Javascript,然后就可以满足需要。
开始下载的功能:
function getDoc() {
self.location='DOCXGenerateDocument.php?templ=finaldemand&invoiceid=<?=$_GET['id']?>';
setTimeout("var s = document.createElement('SCRIPT'); s.src = 'read.js.php'; document.body.appendChild(s);", 100);
}
重新加载脚本:
<?php session_start(); ?>
location.reload();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.