First Step:
Add "printContent()" function onclick with your php code-
<a href="javascript:;" class="btn btn-xs btn-success" onclick="printContent(<?php echo $fetch['id']; ?>);" >Print</a>
<a href="javascript:;" class="btn btn-xs btn-success" onclick="printContent(<?php echo $fetch['id']; ?>);" >Print</a>
Second Step:
Past this jquery code in your same page:
<script>
function printContent(id){
$.ajax({
type: "POST",
url: 'printContent.php',
data: {id: id},
type: 'get',
success: function( response ) {
var contents = response;
var idname = name;
var frame1 = document.createElement('iframe');
frame1.name = "frame1";
frame1.style.position = "absolute";
frame1.style.top = "-1000000px";
document.body.appendChild(frame1);
var frameDoc = frame1.contentWindow ? frame1.contentWindow : frame1.contentDocument.document ? frame1.contentDocument.document : frame1.contentDocument;
frameDoc.document.open();
frameDoc.document.write('<html><head><title></title>');
frameDoc.document.write('<style>table { border-collapse: collapse; border-spacing: 0; width:100%; margin-top:20px;} .table td, .table > tbody > tr > td, .table > tbody > tr > th, .table > tfoot > tr > td, .table > tfoot > tr > th, .table > thead > tr > td, .table > thead > tr > th{ padding:8px 18px; } .table-bordered, .table-bordered > tbody > tr > td, .table-bordered > tbody > tr > th, .table-bordered > tfoot > tr > td, .table-bordered > tfoot > tr > th, .table-bordered > thead > tr > td, .table-bordered > thead > tr > th { border: 1px solid #e2e2e2;} </style>');
// your title
frameDoc.document.title = "Print Content with ajax in php";
frameDoc.document.write('</head><body>');
frameDoc.document.write(contents);
frameDoc.document.write('</body></html>');
frameDoc.document.close();
setTimeout(function () {
window.frames["frame1"].focus();
window.frames["frame1"].print();
document.body.removeChild(frame1);
}, 500);
return false;
}
});
}
</script>
<script>
function printContent(id){
$.ajax({
type: "POST",
url: 'printContent.php',
data: {id: id},
type: 'get',
success: function( response ) {
var contents = response;
var idname = name;
var frame1 = document.createElement('iframe');
frame1.name = "frame1";
frame1.style.position = "absolute";
frame1.style.top = "-1000000px";
document.body.appendChild(frame1);
var frameDoc = frame1.contentWindow ? frame1.contentWindow : frame1.contentDocument.document ? frame1.contentDocument.document : frame1.contentDocument;
frameDoc.document.open();
frameDoc.document.write('<html><head><title></title>');
frameDoc.document.write('<style>table { border-collapse: collapse; border-spacing: 0; width:100%; margin-top:20px;} .table td, .table > tbody > tr > td, .table > tbody > tr > th, .table > tfoot > tr > td, .table > tfoot > tr > th, .table > thead > tr > td, .table > thead > tr > th{ padding:8px 18px; } .table-bordered, .table-bordered > tbody > tr > td, .table-bordered > tbody > tr > th, .table-bordered > tfoot > tr > td, .table-bordered > tfoot > tr > th, .table-bordered > thead > tr > td, .table-bordered > thead > tr > th { border: 1px solid #e2e2e2;} </style>');
// your title
frameDoc.document.title = "Print Content with ajax in php";
frameDoc.document.write('</head><body>');
frameDoc.document.write(contents);
frameDoc.document.write('</body></html>');
frameDoc.document.close();
setTimeout(function () {
window.frames["frame1"].focus();
window.frames["frame1"].print();
document.body.removeChild(frame1);
}, 500);
return false;
}
});
}
</script>
Third Step:
Past this code in request file ("printContent.php")-
<?php
require_once("config.php");
$id=$_REQUEST['id'];
$conn='';
if($id!='all'){
$conn .="and `id`='".$id."'";
}
$select="SELECT * FROM `tblprintcontent` where 1=1 ".$conn." ";
$query=mysql_query($select);
?>
<div style="margin:0 auto;">
<table class="table table-bordered">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php
while($fetch=mysql_fetch_array($query)){
?>
<tr>
<td><?php echo $fetch['firstname']; ?></td>
<td><?php echo $fetch['lastname']; ?></td>
<td><?php echo $fetch['email']; ?></td>
</tr>
<?php
}
?>
</div>
Description:
With this code you can print your content.
For change print page title change " frameDoc.document.title = ' you title here '; " .
IF ANY ERROR:
{
- First check "connection with database"
- Check "$" OR "jquery" error in jquery code.
- Please check jquery library file must be included.
Past this code in request file ("printContent.php")-
require_once("config.php");
$id=$_REQUEST['id'];
$conn='';
if($id!='all'){
$conn .="and `id`='".$id."'";
}
$select="SELECT * FROM `tblprintcontent` where 1=1 ".$conn." ";
$query=mysql_query($select);
?>
<div style="margin:0 auto;">
<table class="table table-bordered">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php
while($fetch=mysql_fetch_array($query)){
?>
<tr>
<td><?php echo $fetch['firstname']; ?></td>
<td><?php echo $fetch['lastname']; ?></td>
<td><?php echo $fetch['email']; ?></td>
</tr>
<?php
}
?>
</div>
Description:
With this code you can print your content.
For change print page title change " frameDoc.document.title = ' you title here '; " .
IF ANY ERROR:
No comments:
Post a Comment