2017-09-05 255 views
0

我有一个将变量传递给fpdf的问题。第一个脚本获取发送到过滤类的过帐文本,该类将过滤后的POST-s作为2元素数组返回。首先剧本是这样的:将变量传递给fpdf

include('service.php'); 
include('pdf.php'); 

    $pdf_filter = new Pdf_filter; 
    $filter = $pdf_filter->pdfFilter(); 
    var_dump($filter); 
    extract($filter); 

我提取$filter阵列从中获取变量(过滤脚本创建被发送POST变量,我可以echo他们,所以我不知道这是不是甚至是必要的)。

第二个脚本是这样的:

require('E:\Xampp\php\fpdf181\fpdf.php'); 

$pdf = new FPDF(); 

$pdf->AddPage(); 
$pdf->SetFont('Arial','B',12); 
$pdf->Cell(195,5, $tytul, 0,1,'C'); 
$pdf->Cell(195,5, $petycja, 0,1,'C'); 
$pdf->Output(); 

,我得到这个错误:

Notice: Undefined variable: tytul in E:\Xampp\htdocs\php\bazy_danych\obiektowe\my\pdf.php on line 10 

Notice: Undefined variable: petycja in E:\Xampp\htdocs\php\bazy_danych\obiektowe\my\pdf.php on line 11 

Fatal error: Uncaught exception 'Exception' with message 'FPDF error: Some data has already been output, can't send PDF file' in E:\Xampp\php\fpdf181\fpdf.php:271 
Stack trace: #0 E:\Xampp\php\fpdf181\fpdf.php(1063): FPDF->Error('Some data has a...') 
#1 E:\Xampp\php\fpdf181\fpdf.php(999): FPDF->_checkoutput() 
#2 E:\Xampp\htdocs\php\bazy_danych\obiektowe\my\pdf.php(12): FPDF->Output() 
#3 E:\Xampp\htdocs\php\bazy_danych\obiektowe\my\test.php(3): include('E:\\Xampp\\htdocs...') 
#4 {main} thrown in E:\Xampp\php\fpdf181\fpdf.php on line 271 

我应该如何传递变量?有趣的:它的工作原理,如果我用的是未经过滤的$_POST用下面的代码:

require('E:\Xampp\php\fpdf181\fpdf.php'); 

$pdf = new FPDF(); 

$pdf->AddPage(); 
$pdf->SetFont('Arial','B',12); 
$pdf->Cell(195,5, $_POST['tytul'], 0,1,'C'); 
$pdf->Cell(195,5, $_POST['petycja'], 0,1,'C'); 
$pdf->Output(); 

编辑:我会后的最初形式和过滤功能:
形式:

<form action="test.php" method="POST"> 
    Wpisz tytuł petycji (35 znaków):<br> 
     <input type="text" name="tytul" maxlength="35" size="35" placeholder="Tytuł petycji" required><br> 
    Wpisz treść petycji (500 znaków):<br> 
     <textarea name="petycja" maxlength="500" rows="4" cols="50" placeholder="Treść petycji" required></textarea><br> 
     <input type="submit" value="Napisz petycje">   
    </form> 

过滤功能:

class Pdf_filter{ 
    protected $title; 
    protected $text; 
     public function pdfFilter(){ 
      if (isset($_POST)){ 

      foreach ($_POST as $key => $val) { 

      $filterVal = strip_tags($val); 
      $filterVal = htmlspecialchars($filterVal); 
      $filterVal = stripslashes($filterVal); 
      $filterVal = str_replace("\\", "", $filterVal); 

      $filter = array($key => $filterVal); 
       foreach ($filter as $key => $val) { 
        echo "[$$key]"; 
        echo "$val<br>"; 
        ${$key} = $val; 
        } 
      } 
     if(!preg_match("/^[\sa-zA-ZĄĆĘŁŃÓŚŹŻąćęłńóśźż0-9-_,.:\'?()]+$/", $tytul)){ 
      echo "Niedozwolone znaki $tytul!"; 
      exit(); 
      } 
      elseif(!preg_match("/^[\sa-zA-ZĄĆĘŁŃÓŚŹŻąćęłńóśźż0-9-_,.:\'?()]+$/", $petycja)){ 
       echo "Niedozwolone znaki $petycja!"; 
       exit(); 
      } 
      else{ 
       return $filter = array('tytul'=>$tytul,'petycja'=>$petycja); 
      } 
    } 
    else{ 
     echo "Proszę wypełnić wszytskie pola!"; 
     } 
    } 
} 
+0

我猜你正在使用'$ tytul = $ _POST ['tytul'];'在第二个脚本中?或者是在其他一些没有给出的脚本中设置?不知道它们是如何设置的,我无法给你一个很好的答案,但很可能你正在使用没有正确使用的类变量。 –

+0

我想你可能是正确的“使用类变量”不正确。但是整个脚本是这样的:2个文本形式(tytul i petycja)通过$ _POST传递给第一个脚本 - >过滤$ _POST - >将过滤的变量返回到第一个脚本 - >将变量传递给第二个脚本。如果您需要更多代码,请让我知道要写什么。 – korni

+0

是的,我想看看如何设置变量。因此,如果您要提供该代码以及该文件如何链接(包括/需要)。 –

回答

0

那么我很笨。问题与类变量有关。这发生在为我工作代码:

class Pdf extends FPDF{ 
     protected $filter; 
     protected $tytul; 
     protected $petycja; 
     public function __construct($filter){ 
      $this->filter = extract($filter); 
      $this->tytul = $tytul; 
      $this->petycja = $petycja; 
     } 
     public function tytul(){ 
      return $this->tytul; 
     } 
     public function petycja(){ 
      return $this->petycja; 
     } 
     public function dokument(){ 
      parent::__construct(); 
      $this->AddPage(); 
      $this->SetFont('Arial','B',15); 
      $this->Cell(195,5, $this->tytul, 0,1,'C'); 
      $this->Cell(195,5, $this->petycja, 0,1,'C'); 
      $this->Output(); 
     } 
} 

现在我需要想办法来显示FPDF和换行符抛光符号(但也有可能只用文本框的文本编辑器isntead完成)。