我有一个小表单,可以获取用户对文档中显示内容的输入。然后我在底部有一个预览按钮和一个保存按钮。预览按钮打开打印窗口以显示文档。我想要的是,在我打印或取消打印预览后,值仍然在表单中。可以这样做吗?这是我的代码。
使用Javascript:
function printFunc() {
var restorepage = document.body.innerHTML;
var printcontent = document.getElementById('printDiv').innerHTML;
document.body.innerHTML = printcontent;
window.print();
document.body.innerHTML = restorepage;
window.close();
return false;
}
function getInfo(){
var title = document.getElementById('title').value;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("dummy").innerHTML = xmlhttp.responseText;
printFunc();
document.getElementById("dummy").style.display = "none";
document.getElementById("printDiv").style.display = "none";
}
}
xmlhttp.open("POST", "try.php", true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("title="+title);
}
原始表格:
Title: <input type="text" id="title">
<div id="dummy"></div>
<input type="button" value="PREVIEW" onclick="getInfo()">
<input type="button" value="SAVE">
AJAX请求:
<?php
$title = $_REQUEST['title'];
?>
<div id="printDiv">
THE TITLE OF THE DOCUMENT IS <?php echo $title;?>
</div>