window.open将参数传递给php并触发pdf创建和查看 [英] window.open to pass parameters to php and trigger pdf creation and view
问题描述
我一直在使用window.open指向一个php文件,在该文件中我使用fpdf即时创建了一个pdf文件,并立即将其显示给用户.似乎可以在所有浏览器甚至移动设备上正常工作.例如:
I've been using window.open to point to a php file where I create a pdf on the fly using fpdf and display it immediately to the user. Seems to work fine across all browsers and even mobile devices. For example:
window.open('/client/feature_Schedule/scheduleReport.php','Schedule_Report','width=900,height=600,status=yes');
在此示例中,我没有传递任何值,因为php文件中已经存在所有需要的参数.
I'm not passing any values in this example because all of the parameters needed already exist in the php file.
但是现在我想从javascript传递值,以便在创建pdf并将其显示给用户之前查询php文件中的数据库.有没有我应该注意的约定? Window.open似乎不处理此问题,只是在?"之后添加值.在网址末尾.但是我更喜欢发布值,因为可能有很多数据发送到php文件.我什至一直在玩$ .ajax,但不确定如何触发pdf以向用户打开.
But now I'd like to pass values from javascript along in order to query of the database in the php file before creating the pdf and displaying it to the user. Is there a convention I should be aware of? Window.open doesn't seem to handle this except by adding the values after "?" at the end of the url. But I'd prefer to POST the values because there could be a lot of data being sent to the php file. I've even been playing around with $.ajax but am not sure how to trigger with pdf to open for the user.
谢谢.
推荐答案
有一种使用window.open()发送POST数据的解决方案,它很简单: Window.open并通过post方法传递参数
There is a solution to send POST data with window.open() and it is quite simple: Window.open and pass parameters by post method
甚至可以在不进行任何纯HTML程序编写的实际HTML代码的情况下进行制作.
It can even be made without any actual HTML code involved just with pure javascript programming.
//添加了仅javascript解决方案:
// added javascript only solution:
<input type="button" value="Open" onclick="openWindow();" />
<script>
function openWindow(){
// create <form>
var form = document.createElement('form');
form.method = 'POST';
form.action = 'process.php';
form.target = 'window_1'; // target the window
// append it to body
var body = document.getElementsByTagName('body')[0];
body.appendChild(form);
// create an element
var child1 = document.createElement('input');
child1.type = 'text'; // or 'hidden' it is the same
child1.name = 'elem1';
child1.value = 'some value';
// append it to form
form.appendChild(child1);
// create another element
var child2 = document.createElement('input');
child2.type = 'text'; // or 'hidden' it is the same
child2.name = 'elem2';
child2.value = 'some other value';
// append it to form
form.appendChild(child2);
// create window
window.open('', 'window_1');
// submit form
form.submit();
body.removeChild(form); // clean up
}
</script>
这篇关于window.open将参数传递给php并触发pdf创建和查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!