HTML表单调用返回附件的PHP页面; 如何将“控件”返回到HTML页面?
All,
I have an HTML page that contains a form.
When the user completes the form and clicks "Submit" - the form posts the user's inputs to a PHP page.
That PHP page processes the inputs and returns the user's results as an attachment.
So far - everything works OK - the user clicks "Submit", the browser's progress bar starts to "spin", and after a few seconds, the attachment is downloaded.
However, in the browser - the user is still on the form page. That's OK with me.
So - here's my question. Ideally, when the user clicks "Submit" - I would like to update the form page to indicate that the "process" is in progress. E.g.,
- disable the submit button to prevent redundant clicks
- display a "downloading" message and a "spinner"-type graphic
Then, when the download is complete - I'd like to capture that event so that I could re-enable the submit button and remove the downloading message.
Is this possible?
Or - is my initial set-up just wrong? Is there a better way to do this? (That is - have a form that calls a PHP page, have the PHP page return an attachment, and then return control to the calling page?)
Thanks in advance for your advice and insight!
全部, p>
我有一个包含表格的HTML页面。
当用户完成表单并单击“提交”时 - 表单将用户的输入发布到PHP页面。 p>
该PHP页面处理输入 并返回用户的结果作为附件 strong>。 p>
到目前为止 - 一切正常 - 用户点击“提交”,浏览器的进度条开始“旋转” ,几秒钟后,下载附件。 p>
但是,在浏览器中 - 用户仍在表单页面上。 那对我没问题。 p>
所以 - 这是我的问题。 理想情况下,当用户点击“提交”时 - 我想更新表单页面以指示“进程”正在进行中。 例如, p>
然后,当下载完成时 - 我想捕获该事件,以便我可以重新启用提交按钮并删除下载消息。 p>
这可能吗? p>
或者 - 我的初始设置是错的吗? 有一个更好的方法吗? (那是 - 有一个调用PHP页面的表单,让PHP页面返回一个附件,然后将控制返回到调用页面吗?) p>
提前感谢您的建议和见解 ! p>
div>
Use javascript to open up the download in a new window. So don't return the download in the POST request, simply let the page reload and put some javascript on the page that will open up a new window that will call your PHP script that will download the file.
I'm not sure there would be any event regarding the downloading/streaming files via http (consider that it happens totally on the server side), however, if I were you I would redirect the form to the download page and then after web server starts streaming the file to the client's browser, I would redirect back to the form page again.
You can achieve that both via PHP and Javascript. :)
In order to get this kind of functionality (disabling submit button, showing a spinner or progress bar) you will need to use some JavaScript. I would recommend the JQuery Ajax Form plugin: http://www.malsup.com/jquery/form/.
The tricky part with using JS and AJAX is that it will be hard to have the user download the file as an attachment.
To use purely HTML and PHP I would recommend re-directing to another page on submit, displaying some message (..."processing"...) and then redirecting back to the form page where the page will load, prompt for the download, and then show the form again.
A graceful way to handle this , might be through ajax.
a) On you final page when you press "Submit" button , you post an ajax request to
- build the document dynamically and or generate link to download document
- Return the download link in ajax response
b) On ajax response , you trigger the download link e.g
//jquery code
$.ajax ({...
success:function(resp){
window.open(resp.FILE);
// to the rest of work here
// e.g enable disable submit button
})