2012-03-28 35 views
0

我通過jQuery將用戶數據發送到TCPDF,但它不顯示從PDF文件中接收到的來自jQuery的任何值。TCPDF沒有通過jQuery捕獲PHP變量

我甚至試圖捕獲會話變量中的值,但它仍然不顯示。我哪裏錯了?

HTML:

<div id="p"> 
    FOO 
    <input type="text" id="nop" /> 
</div> 

<div id="e_table"> 
       <table> 
        <thead> 
         <th><h3>ABC</h3></th> 
         <th><h3>PQR</h3></th> 
         <th><h3>XYS</h3></th> 
        </thead> 
        <tbody> 
        <tr> 
         <td>Staff</td> 
         <td><input type="text" id="as" /></td> 
         <td><label id="las">label</label></td> 
        </tr> 
        <tr> 
         <td>Office</td> 
         <td><input type="text" id="bo"/></td> 
         <td><label id="lbo">label</label></td> 
        </tr> 
        <tr> 
         <td>Administrative</td> 
         <td><input type="text" id="mca" /></td> 
         <td><label id="lmca">label</label></td> 
        </tr> 
        </tbody> 
       </table> 
</div> 

的jQuery:

$('#pdf').click(function() {      
        var nop = $('#nop').val(); 
        var as = $('#as').val(); 
        var las = $('#las').text(); 
        var bo = $('#bo').val(); 
        var lbo = $('#lbo').text(); 
        var mca = $('#mca').val(); 
        var lmca = $('#lmca').text(); 

        var postData = { 
         "nop" : nop, 
         "as" : as, 
         "las" : las, 
         "bo" : bo, 
         "lbo" : lbo, 
         "mca" : mca, 
         "lmca" : lmca 
        }; 

        $.ajax({ 
          type: "POST", 
          url: "pdf.php", 
          data: postData, 
          success: function(data){ 
           window.open("pdf.php"); 
          } 
         }); 


     }); 

TCPDF pdf.php:

<?php 
session_start(); 

//============================================================+ 
// Author: Nicola Asuni 
// 
// (c) Copyright: 
//    Nicola Asuni 
//    Tecnick.com s.r.l. 
//    Via Della Pace, 11 
//    09044 Quartucciu (CA) 
//    ITALY 
//    www.tecnick.com 
//    [email protected] 
//============================================================+ 

/** 
* @author Nicola Asuni 
* @since 2009-03-20 
*/ 

require_once('tcpdf/config/lang/eng.php'); 
require_once('tcpdf/tcpdf.php'); 

// Extend the TCPDF class to create custom Header and Footer 
class MYPDF extends TCPDF { 


    // Page footer 
    public function Footer() { 
     // Position at 15 mm from bottom 
     $this->SetY(-15); 
     // Set font 
     $this->SetFont('helvetica', 'B', 8); 
     $this->SetTextColor(247, 147, 29); 


     $this->Cell(0, 10, 'ABC', 0, false, 'C', 0, '', 0, false, 'T', 'M'); 
    } 
} 

// create new PDF document 
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); 

// set document information 
$pdf->SetCreator(PDF_CREATOR); 
$pdf->SetAuthor('ABC'); 
$pdf->SetTitle('ABC'); 
$pdf->SetSubject('ABC'); 
$pdf->SetKeywords('ABC'); 


// set default header data 
$pdf->SetHeaderData(false); 


$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); 
// set default monospaced font 
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); 

//set margins 
$pdf->SetMargins(PDF_MARGIN_LEFT, 5, PDF_MARGIN_RIGHT); 
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER); 
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER); 

//set auto page breaks 
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); 

//set image scale factor 
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); 

//set some language-dependent strings 
$pdf->setLanguageArray($l); 
// --------------------------------------------------------- 


// add a page 
$pdf->AddPage(); 

$pdf->SetFont('helvetica', '', 8); 

// ----------------------------------------------------------------------------- 


//request parameters 
    $nop = strip_tags($_POST['nop']); 
    $_SESSION['nop'] = $nop; 
    $nop = $_SESSION['nop']; 
    $as = strip_tags($_POST['as']); 
    $las = strip_tags($_POST['las']); 
    $bo = strip_tags($_POST['bo']); 
    $lbo = strip_tags($_POST['lbo']); 
    $mca = strip_tags($_POST['mca']); 
    $lmca = strip_tags($_POST['lmca']); 

$tbl = <<<EOF 
      <div> 
       <table border="0" cellpadding="5" cellspacing="2"> 
        <tr> 
         <td>FOO</td> 
         <td>$nop</td> 
        </tr> 
        </tbody> 
       </table> 
       </div> 
       <hr> 
       <div> 
       <table border="0" cellpadding="5" cellspacing="2"> 
        <thead> 
        <tr> 
         <th><h3>ABC</h3></th> 
             <th><h3>PQR</h3></th> 
             <th><h3>XYS</h3></th> 
        </tr> 
        </thead> 
        <tbody> 
        <tr> 
         <td>Staff</td> 
         <td>$as</td> 
         <td>$las</td> 
        </tr> 
        <tr> 
         <td>Office</td> 
         <td></td> 
         <td></td> 
        </tr> 
        <tr> 
         <td>Administrative</td> 
         <td></td> 
         <td></td> 
        </tr> 
        </tbody> 
       </table> 
       </div> 
     </div> 
EOF; 

$pdf->writeHTML($tbl, true, false, false, false, ''); 

$txt = $nop . 'This'; 

// print a block of text using Write() 
$pdf->Write($h=0, $txt, $link='', $fill=0, $align='C', $ln=true, $stretch=0, $firstline=false, $firstblock=false, $maxh=0); 
// ----------------------------------------------------------------------------- 


//Close and output PDF document 
$pdf->Output('ABC.pdf', 'I'); 

//============================================================+ 
// END OF FILE             
//============================================================+ 

回答

1

您所呼叫的pdf.php 2倍,一個與職位數據,一個沒有

$.ajax({ // call 1, with post data 
    type: "POST", 
    url: "pdf.php", 
    data: postData, 
    success: function(data){ 
     window.open("pdf.php"); // call 2, without post data 
    } 
}); 

你可以做的是刪除您的Ajax請求,並通過GET與

var getString = "nop=" + nop "&as=" + as; // ..... 
window.open("pdf.php?" + getString); 
+0

感謝您的幫助打開。這工作。我只想知道我是否可以隱藏url字符串中的參數? – input 2012-04-09 20:18:00

+0

@input不好意思,不要拿。取決於你想隱藏的原因,但是很容易查看發送到的發佈值 – trembon 2012-04-10 06:43:32

0

嘗試 的print_r($ _ POST);

並把它放在你建立pdf的php文件的開始處。 這有助於你瞭解哪些值到達和值不

0

發送數據,而不是我甚至試圖捕捉在會話變量中的值,但它仍然不會顯示。我哪裏錯了?

它應該工作。你錯過了session_start()

另一種方法是生成PDF在您ajax請求和輸出文件名,你將與window.open()