1

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>
Jamal Naqvi
  • 13
  • 1
  • 4
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Daan Mar 31 '15 at 12:04
  • @Daan I've checked this question already but I'm unable to solve this by using answer given. – Jamal Naqvi Mar 31 '15 at 12:13

2 Answers2

2

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.

Arlind Hajredinaj
  • 8,380
  • 3
  • 30
  • 45
0

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"/>
Gershom Maes
  • 7,358
  • 2
  • 35
  • 55
  • I've added this and it is confirmed that my form isn't submitted so how to solve this problem. – Jamal Naqvi Mar 31 '15 at 12:33
  • In that case, this is the answer you've asked for. Perhaps if the form is not submitted you will want to avoid outputting a pdf entirely. In this case you can wrap the entire pdf generating code in the 1st block of the if statement. You'll need to post a new question that's more specific to the problem you're now having, because this question only deals with how $s_name was not being properly initialized. – Gershom Maes Mar 31 '15 at 12:36