我在jquery和ajax方面不是很有經驗,但我需要以下內容。用戶成功登錄後,他們可以從該頁面下載PDF文件。該網站有一個崩潰div,他們可以從中選擇要下載的文件。一切都在PHP中完成,並行得通。但是,如果找不到文件,摺疊div將被關閉。換句話說,網頁被重新加載。因此,用戶必須新作出新的選擇,這是不方便的。所以我會以下。如果找不到該文件,那麼div應該保持摺疊狀態,並在表單上的輸入字段's'中保留以前的值。我知道這應該通過jQuery或JavaScript來完成。我嘗試了以下,但它不起作用。在php post方法中使用jquery
//PHP
<?php
//if download is clicked
if (isset($_POST["download"])) {
$data = $_POST["s"];
//if file is found
if (fileexists($data){
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The PDF source is in original.pdf
readfile($data);
}
//else the div should remain open
else{
echo "<script> if ($('#collapseOne').is(':hidden')) {
$('#collapseOne').show();
}</script>";
}
}
?>
<div id="collapseOne" class="panel-collapse collapse">
<div class="panel-body">
<form id='searchForm' method="post" action=''>
<p><label>Select Month</label></p>
<input id='s' name = 's' type='text' required><br><br>
<button type="submit" id='download' name='download' class='btn btn-default btn-lg'>
<span class='glyphicon glyphicon-download-alt'></span> Download
</button>
<br> <br>
<button id='download1' name = 'download1' class='btn btn-default btn-lg'>
<span class='glyphicon glyphicon-download-alt'></span> Download All
</button>
</form>
</div>
</div>
的可能重複[如何從PHP傳遞變量和數據的JavaScript?](http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php -to-javascript) – 2014-08-31 14:13:05