2013-05-09 37 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