简体   繁体   中英

TCPDF won't recognize $_POST

I'm trying to create a pdf that receives data via POST,I know that the data is being received because i tested using "var_dump($_POST)".

Result:

array (size=9)
 'orcCar' => string 'S' (length=1)
 'contItem' => 
  array (size=1)
  0 => string '1' (length=1)
 'codProduto' => 
  array (size=1)
  0 => string '000zxxxxxxx' (length=14)
 'qtdProduto' => 
  array (size=1)
  0 => string '20' (length=2)
'prcuProduto' => 
array (size=1)
  0 => string '4.28' (length=4)
'prctProduto' => 
array (size=1)
  0 => string '85.60' (length=5)
'descProduto' => 
array (size=1)
  0 => string 'sdsudhudud' (length=33)
'countNitens' => string '2' (length=1)
'codClientecopia' => string '' (length=0)

But when i try to use it in the middle of the html code or in a loop it wont work.

This is part of the code:

  for($i=0; $i < count($_POST["codProduto"]); $i++)
  {
     if ($_POST["prcuProduto"][$i]=="")
     {
     $_POST["prcuProduto"][$i] = '0';
     }
  $contador=$_POST["contItem"][$i];

  // Set some content to print

  $html.="<tr>
  <td style='width:5%;'><input type='number' name='contItem[]' 
  style='width:100%'id='contItem' readonly='readonly' value=".$contador." 
  maxlength='5'></td>

  <td style='width:20%;'><input type='text' name='codProduto[]'  
  style='width:100%'id='codProduto' readonly='readonly'  maxlength='20' 
  value=". $_POST['codProduto'][$i]."></td>";

   }
   // Print text using writeHTMLCell()
  $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);`enter code here`

It won't enter the loop because of

count($_POST["codProduto"])

when changed with value it works, but it still won't show any values or the "td".I also tried creating variables with the values from the post but it still didin't work.

Could someone help me how to use velues recived from post in tcpdf?

I recreated your POST object and it enters the loop just fine for me. There's a couple of things to note here.

First, input is not a supported tag for the TCPDF HTML parser. If you're just wanting to add boxes around the field values, then add a border to the td instead.

Secondly, TCPDF's HTML parser is pretty fragile. You need to make sure you include all the necessary HTML tags. For example, in your code the contents of $html are not wrapped in a table tag, and there's no </tr> tag for the rows. TCPDF also needs all HTML attributes to be wrapped in double quotes .

In my tests with TCPDF 6.2.17, the following snippet works:

$html = '<table cellpadding="2">';
//I'm adding a border on the cells, and TCPDF doesn't support CSS padding
//so we'll use table's cellpadding attribute. Not strictly required, but
//I thought it looked nice.
for($i=0; $i < count($_POST["codProduto"]); $i++)
{
     if ($_POST["prcuProduto"][$i]=="")
     {
     $_POST["prcuProduto"][$i] = '0';
     }
     $contador=$_POST["contItem"][$i];

  // Set some content to print

  $html.="<tr>
  <td style=\"width:5%; border: 1px solid black; \">$contador</td>

  <td style=\"width:20%; border: 1px solid black; \">{$_POST['codProduto'][$i]}</td></tr>";
  //Make sure we have our ending </tr> tag and wrap the style attributes in double
  //quotes so TCPDF will actually pay attention to them.
   }
  $html .= '</table>';
  // End our table and print text using writeHTMLCell()
  $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM