2015-03-31 30 views
1

我使用FPDF以pdf格式顯示以html格式提交的數據。 我已經完成了前面提到的所有解決方案,但是我無法解決這個問題。 我發現了錯誤:如何在FPDF中刪除此錯誤?

Notice: Undefined variable: s_ name in C:\xampp\htdocs\dynamic website\form1.php on line 9 FPDF error: Some data has already been output, can't send PDF file

我的HTML代碼部分是

<td align="right" valign="top"> 
    <label for="student3_name">3.Name </label> 
    </td> 
<td valign="top"> 
    <input type="text" name="student3_name" maxlength="50" size="20" > 
    </td> 

和我form1.php如下:

<?php 
    if(isset($_POST['submit'])) 
    {$s_name = $_POST["student3_name"];} 
    require ("fpdf/fpdf.php"); 
    $pdf=new FPDF('P','mm','A4'); 
    $pdf->AddPage(); 
    $pdf->SetFont('Arial','B',16); 
    $pdf->Cell(0,50,'',0,1); 
    $pdf->Cell(60,10,"hello{$s_name}",10,8,'C'); 
    $pdf->output(); 
?> 

編輯: 這裏是詳細部分我的表格

<form name="submission_form" method="post" action="form1.php"> 
    <p style="text-align: center; font-size: 24px; font-weight: 900;></p> 
    <table width="634" align="center" border="1"> 
    <tr> 
    <td align="right" valign="top"> 
    <label for="student3_name">3.Name </label> 
    </td> 
    <td valign="top"> 
    <input type="text" name="student3_name" maxlength="50" size="20" > 
    </td> 
    </tr> 
    <tr> 
    <td colspan="4" style="text-align:center"> 
    <input type="submit" value="Submit"></td> 
    </tr> 
+0

[PHP:「通知:未定義變量」和「通知:未定義指數」]的可能重複(http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Daan 2015-03-31 12:04:32

+0

@Daan我已經檢查了這個問題,但是我無法使用給出的答案來解決這個問題。 – 2015-03-31 12:13:49

回答

1

你的代碼改成這樣:

<?php 
     $s_name = ""; 
     if(isset($_POST['submission_form'])) 
     { 
       $s_name = $_POST["student3_name"]; 
     } 
     require ("fpdf/fpdf.php"); 
     $pdf=new FPDF('P','mm','A4'); 
     $pdf->AddPage(); 
     $pdf->SetFont('Arial','B',16); 
     $pdf->Cell(0,50,'',0,1); 
     $pdf->Cell(60,10,"hello{$s_name}",10,8,'C'); 
     $pdf->output(); 
    ?> 

更改您的提交按鈕,這

<input name="submission_form" type="submit" value="Submit"> 

您聲明塊內部$ S_NAME變量,該變量是塊無形之外,所以你不能使用它在除聲明塊之外的其他任何地方。

+0

謝謝你現在錯誤被刪除,但在我的pdf文件中,我看不到s_name的值。我輸入student3_name但它不顯示。 – 2015-03-31 12:11:58

+0

什麼是你的表單方法?是GET還是POST? – 2015-03-31 12:14:04

+0

這個條件isset($ _ POST ['submit'])返回false這就是爲什麼。你能告訴我你提交的表格嗎? – Arlind 2015-03-31 12:15:23

0

如果isset($_POST['submit'])返回false,則需要一個其他條件來定義s_name

喜歡的東西:

if (isset($_POST['submit'])) { 
    $s_name = $_POST['student3_name']; 
} else { 
    $s_name = 'The form hasn\'t been submitted yet!'; 
} 

跟上您的編輯:

還要確保提交按鈕被髮布它的價值。爲了讓這種情況發生,你需要包括名稱屬性:

<input name="submit" type="submit" value="Submit"/> 
+0

我已經添加了這個,並確認我的表單沒有提交,所以如何解決這個問題。 – 2015-03-31 12:33:41

+0

在這種情況下,這是您要求的答案。也許如果表單沒有提交,你會希望避免完全輸出pdf。在這種情況下,您可以將整個pdf生成代碼包裝在if語句的第一個塊中。 您需要發佈一個更具體的問題來解決您現在遇到的問題,因爲此問題僅涉及$ s_name未正確初始化的問題。 – 2015-03-31 12:36:19