Jamal Naqvi
Jamal Naqvi

Reputation: 13

How to remove this error in FPDF?

I'm using FPDF to show the data submitted in html form in pdf. I've gone through all the solutions given on previous questions but I'm unable to solve this. I'm getting the error:

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

Part of my html code is

<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>

and my form1.php is as follows:

<?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();
?>

EDIT: here is the detailed part my form

<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>

Upvotes: 1

Views: 4229

Answers (2)

Gershom Maes
Gershom Maes

Reputation: 8178

You need an else condition that will define s_name in the case that isset($_POST['submit']) returns false.

Something like:

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

Keeping up with your edits:

Also ensure that the submit button is posting its value. In order for this to occur you need to include a name attribute:

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

Upvotes: 0

Arlind Hajredinaj
Arlind Hajredinaj

Reputation: 8519

Change your code to this:

<?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();
    ?>

Change your submit button to this

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

You are declaring $s_name variable inside a block, that variable is invisible outside of the block so you cannot use it anywhere else except inside the block where it is declared.

Upvotes: 2

Related Questions