2015-03-31 106 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