unfortunately this question may get closed but i hope some people answer it.
I have some PHP code that opens excel with some html data in it. This is the PHP:
<?php
$data = $_REQUEST['datatodisplay'];
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=Data.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo $data;
?>
And this is the form:
<form class='noPrint' action='demo/saveToExcel.php' method='post' target='_blank'
onsubmit="$('#datatodisplay').val( $('<div>').append( $('#dataTable').eq(0).clone() ).html() )">
<input id='excel' type='image' src='img/file.png'>
<input type='hidden' id='datatodisplay' name='datatodisplay' />
</form>
Now, this code works great, but could someone explain from a 开发者_JS百科PHP perspective how it does this, ie. i submit the form, it opens a new page temporarily, comes up with a download box, i hit open in Excel and then i'm sent back to the form page (but with Excel also open)
How does PHP know that i've actioned the download and to return to the form page?
It doesn't. Your browser never left the form page in the first place, since the file was downloaded instead of shown in the browser.
PHP doesn't know - all it's doing is serving an excel file. The clever bit is happening in the JavaScript happening on the onsubmit
method of the form
精彩评论