下载文件后,数据库中没有数据

时间:2013-05-09 15:09:36

标签: mysql phpexcel

我希望在用户输入您要显示的数据后,在表格的特定条件下显示数据。 但总是无法显示数据。我很困惑,错误所在。 它看起来像变量$ thn = $ _POST ['thn'],$ bln = $ _POST ['bln'],$ periode = $ _POST ['periode']为空。 请帮忙。

我有四个文件。 代码如下:

1.absen_xls.php:

<?php
include '../../inc/inc.koneksi.php';
ini_set('display_errors', 1); ini_set('error_reporting', E_ERROR);
include '../../excel/PHPExcel.php';
include '../../excel/PHPExcel/IOFactory.php';

$table   = 'absen_karyawan';
$bln     = $_POST['bln'];     //this not work, I don't know why?
$thn     = $_POST['thn'];     //this not work, I don't know why?
$periode = $_POST['periode']; //this not work, I don't know why?
$where   = "WHERE tahun = '$thn' AND bulan = '$bln' AND periode = '$periode'";

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();


$sql    = "SELECT namakaryawan,tahun,bulan,periode,absen FROM $table $where";
$query  = mysql_query($sql);

// bold
$objPHPExcel->getActiveSheet()->getStyle("A2:C2")->applyFromArray(array("font" => array( "bold" => true)));

// Add some data
$objPHPExcel->setActiveSheetIndex(0)
 ->setCellValue('A2', 'No')
 ->setCellValue('B2', 'Nama')
 ->setCellValue('C2', 'Kehadiran');

$baris = 3; 
$no = 0;            
while($row=mysql_fetch_array($query)){
$no = $no +1;
$objPHPExcel->setActiveSheetIndex(0)
 ->setCellValue("A$baris", $no)
 ->setCellValue("B$baris", $row['namakaryawan'])
 ->setCellValue("C$baris", $row['absen']);
$baris = $baris + 1;
}

//border
$border = $baris - 1;
$styleArray = array(
  'borders' => array(
'allborders' => array(
  'style' => PHPExcel_Style_Border::BORDER_THIN
)
  )
);
$objPHPExcel->getActiveSheet()->getStyle('A2:C'.$border)->applyFromArray($styleArray);
unset($styleArray);

//width
$objPHPExcel->getActiveSheet()->getColumnDimension("A")->setWidth(5);
$objPHPExcel->getActiveSheet()->getColumnDimension("B")->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension("C")->setWidth(15);
//align center
$objPHPExcel->getActiveSheet()->getStyle('A2:C'.$border)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
// Rename sheet
$objPHPExcel->getActiveSheet()->setTitle('Absen');

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excelformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="absen.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
?>

2.ajax.php:

// JavaScript Document
$(document).ready(function(){
    $(function(){
        $('button').hover(
            function() { $(this).addClass('ui-state-hover'); }, 
            function() { $(this).removeClass('ui-state-hover'); }
    );
});
$("#tampil_data").load('modul/lap-absen/tampil_data.php');

$("#print").click(function(){
    var thn     = $("#thn").val();
    var bln     = $("#bln").val();
    var periode = $("#periode").val();
    cetakabsen(thn,bln,periode);
});
function cetakabsen(c1,c2,c3){
    var thn = c1;
    var bln = c2;
    var periode = c3;

    $.ajax({
        type    : "POST",
        url     : "modul/lap-absen/absen_xls.php",
        data    : "thn="+thn+"&bln="+bln+"&periode="+periode,
        success : function(data){
            window.open('http://localhost/gudang/modul/lap-absen/absen_xls.php'); 
        }
    });
}
});

1 个答案:

答案 0 :(得分:1)

将您的功能更改为:

function cetakabsen(c1,c2,c3){
var thn = c1;
var bln = c2;
var periode = c3;

var  data  = "thn="+thn+"&bln="+bln+"&periode="+periode;

window.open('http://localhost/gudang/modul/lap-absen/absen_xls.php?'+data); 

}

并在absen_xls.php中接收您的值:

$bln     = mysql_real_escape_string($_GET['bln']);     
$thn     = mysql_real_escape_string($_GET['thn']);     
$periode = mysql_real_escape_string($_GET['periode']); 

PS:转到mysqli或PDO,不要使用mysql扩展名。