0

I want to display the data on a particular condition of the table after the user enters the data you want to display. But always failed to display the data. I am confused where the fault lies. It looks like the variable $thn = $ _POST['thn'], $bln = $ _POST['bln'], $periode = $ _POST['periode'] is empty. please help.

I have four files. Here the codes:

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'); 
        }
    });
}
});
4

1 回答 1

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 扩展。

于 2013-05-09T16:09:45.120 回答