2013-05-09 102 views
0

我想在用戶輸入要顯示的數據後,在表格的特定條件下顯示數據。 但總是無法顯示數據。我很困惑錯誤在哪裏。 它看起來像變量$ 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'); 
     } 
    }); 
} 
}); 
+0

你是否真的通過POST提交表單?例如'

'?你很容易受到[SQL注入攻擊](http://bobby-tables.com)的影響,所以在你使用這段代碼做任何其他事情之前,先了解這些。 – 2013-05-09 15:11:05

+0

當你這樣說,沒有工作,你的意思是你收到空值...顯示你的數據表單,或者你武裝你的發佈數組的方式 – Hackerman 2013-05-09 15:11:58

+0

Marc B:我已經完成了,但仍然沒有顯示數據文件下載後。 – user2366659 2013-05-09 15:18:37

回答

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擴展名。

+0

其工作...!非常感謝你... :) 我已經把'','''改成'';'''on'var data =「thn =」+ thn +「&bln =」+ bln +「&periode =」+ periode, '和'mysql_real_scape_string'到'mysql_real_escape_string' – user2366659 2013-05-09 17:06:07

+0

Ooops,我的錯誤....我更新我的答案與您的代碼更改。 – Hackerman 2013-05-09 17:12:51

+0

好的羅伯特,謝謝.... :) – user2366659 2013-05-09 17:17:40