我在一个项目中,我必须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>
这是我的想法,但我认为它不起作用。所以,现在该怎么办?请帮帮我。
由于
答案 0 :(得分:1)
通过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>
答案 1 :(得分:0)
试试这个
(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用于触发window.print之前的操作 afterPrint用于打印后的操作
请注意,这不是插入数据的可靠方式。我在打印后将其用于页面重定向
答案 2 :(得分:0)
试试这个
<script>
$(document).ready(function(){
$("#printbutton").click(
$.ajax({
url: "yourPage.php",//your inserted query page url
success: function(data){
$("#responseArea").text(data);
}
});
);
});