我在一个项目中,我必须print a page
,然后我在表中有insert
一些数据。我正在使用JavaScript函数print()(window.print())
打印页面,但我如何在打印后插入数据?我正在寻找一个更容易的&聪明。
这是我的代码:
<?php
$con=mysqli_connect("localhost","user","pass","db_name");
if(mysqli_connect_errno())
echo "Failed to connect to MySQL: ".mysqli_connect_error();
$result=mysqli_query($con,"SELECT * FROM table_name");
?>
<html>
<head>.......</head>
<body>
<?php while($row=mysqli_fetch_array($result)){?>
.
.
.
//some html code
<button id="printbutton" value="print" onClick="window.print();">PRINT</button>
<script>
$(document).ready(function(){
$("#printbutton").click(
<?php
mysqli_query($con,"Insert into table_name .............");
mysqli_close($con);
?>
);
});
</script>
这是我的主意,但它不起作用,正如我所想的。那么,现在我该怎么办呢?请帮帮我。
谢谢
您最好通过ajax提交表单。例如:
<button id="printbutton" value="print" onClick="window.print();">PRINT</button>
<script>
$(document).ready(function(){
$("#printbutton").click(
// get your variables you want to post
varName = varValue;
// issue an AJAX request with HTTP post to your server side page.
$.post("test.aspx", { varName: varName},
function(data){
// callack function gets executed
alert("Return data" + data);
});
// to prevent the default action
return false;
);
});
</script>
试试这个
(function() {
var beforePrint = function() {
};
var afterPrint = function() {
// do ajax for insert queries
};
if (window.matchMedia) {
var mediaQueryList = window.matchMedia('print');
mediaQueryList.addListener(function(mql) {
if (mql.matches) {
beforePrint();
} else {
afterPrint();
}
});
}
window.onbeforeprint = beforePrint;
window.onafterprint = afterPrint;
}());
beforePrint用于窗口之前的操作。触发打印afterPrint用于打印
之后的操作注意,这不是一种可靠的插入数据的方式。我使用这个页面重定向后打印
试试这个
<script>
$(document).ready(function(){
$("#printbutton").click(
$.ajax({
url: "yourPage.php",//your inserted query page url
success: function(data){
$("#responseArea").text(data);
}
});
);
});