【问题标题】:$_POST Multiple Values from HTML form$_POST HTML 表单中的多个值
【发布时间】:2020-11-22 10:06:57
【问题描述】:

我正在尝试使用 FPDF 从 html 表单创建 PDF。

一切都很顺利,除了一组复选框:

HTML:

<div class="form-group checkboxes" required>
  <p>Que aulas pretende frequentar?</p><span class="clue"> (Selecione as opções aplicáveis)</span></p>
  <label for="pre-ballet">
    <input
            name="aulas[]"
            id="pre-ballet"
            type="checkbox" 
            value="pre-ballet"
            /><span> Pre-Ballet/ Dança Criativa - 3 aos 6 anos</span></label>
            
    <label for="classic">
        <input
            name="aulas[]"
            id="classic"
            type="checkbox"
            value="classic"
            /><span> Técnica de Dança Clássica - Primary/ Iniciantes - 6 aos 9 anos / 10 aos 12 anos / mais de 12 anos</span></label>
    <label for="contemporary">
    <input  
            name="aulas[]"
            id="contemporary"
            type="checkbox"
            value="contemporary"
            /><span> Técnica de Dança Contemporânea - 8 aos 14 anos / mais de 14 anos</span></label></div>

PHP:

if(isset($_POST['sendemail']))
{
    
    require('C:\Users\mcane\scoop\apps\apache\2.4.43\htdocs\PHP\vendor\fpdf\fpdf.php');
    $title = 'Inscrição';
    $name = $_POST['name'];
    $email_id = $_POST['email'];
    $age = $_POST['age'];
    $aulas = $_POST['aulas'];

// create PDF
    $pdf = new FPDF();
    $pdf -> AddPage();
    $pdf->SetTitle($title);
    // Arial bold 15
    $pdf->SetFont('Arial','B',15);
    // Calculate width of title and position
    $w = $pdf->GetStringWidth($title)+6;
    $pdf->SetX((210-$w)/2);
    // Colors of frame, background and text
    $pdf->SetDrawColor(221,221,221,1);
    $pdf->SetFillColor(10,158,0,1);
    $pdf->SetTextColor(255,255,255,1);
    // Thickness of frame (1 mm)
    $pdf->SetLineWidth(1);
    // Title
    // Cell(width, height, content, border, nextline parametters, alignement[c - center], fill)
    $pdf->Cell($w, 9, $title, 1, 1, 'C', true);
    // Line break
    $pdf->Ln(10);

    $pdf->SetTextColor(0,0,0,1);
    $w = $pdf->GetStringWidth($name)+30;
    $pdf->SetX((170-$w)/2);
    $pdf->Cell(40, 10, 'Nome:', 1, 0, 'C');
    $pdf->Cell($w, 10, $name, 1, 1, 'C');

    $pdf->SetX((170-$w)/2);
    $pdf->Cell(40, 10, 'Email:', 1, 0, 'C');
    $pdf->Cell($w, 10, $email_id, 1, 1, 'C');

    $pdf->SetX((170-$w)/2);
    $pdf->Cell(40, 10, 'Idade:', 1, 0, 'C');
    $pdf->Cell($w, 10, $age, 1, 1, 'C');
    
    $pdf->SetX((170-$w)/2);
    $pdf->Cell(40, 10, 'Aulas:', 1, 0, 'C');
    $pdf->Cell($w, 10, $aulas, 1, 1, 'C');

当我提交表单时,会生成 PDF,但我收到此错误消息 - 警告:iconv() 期望参数 3 是字符串,C:... 中给出的数组 - 并且“aulas”单元格为空.

有人可以帮帮我吗?

最好的问候。

【问题讨论】:

    标签: php html post


    【解决方案1】:

    您的checkbox 名称中的[] 被PHP 解释为一个数组,并允许您将复选框组的多个值组合在一起。 $_POST['aulas'] 不是你期望的字符串,而是一个数组。

    当您调用 $pdf-&gt;Cell() 时,FPDF 在内部使用 iconv 库,它需要一个字符串,而不是您提供的数组。这是抛出错误。

    根据您希望在 PDF 中呈现数据的方式,只需更改以下内容即可:

    $pdf->Cell($w, 10, $aulas, 1, 1, 'C');
    

    到这里:

    // Glue all `$aulas` array elements together as a string, separating each value by a comma and space.
    $pdf->Cell($w, 10, implode(', ', $aulas), 1, 1, 'C');
    

    【讨论】:

      【解决方案2】:
      $name = strval(filter_input(INPUT_POST, 'name'));
      $email_id = strval(filter_input(INPUT_POST, 'email'));
      $age = strval(filter_input(INPUT_POST, 'age'));
      $aulas = filter_input(INPUT_POST, 'aulas', FILTER_SANITIZE_STRING, FILTER_REQUIRE_ARRAY);
      $aulas = empty($aulas) ? '' : implode(', ', $aulas);
      
      1. 不要使用 $_POST。使用 filter_input。
      2. 如果未选中复选框,则不会在 $_POST 中返回复选框(未选中任何选项时,$aulas 设置为空字符串)。
      3. 将空值转换为空字符串。您可能需要更改年龄以确保它是一个整数。
      4. 将数组转换为内爆字符串(在示例中使用 ', ' 作为胶水)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-09
        • 1970-01-01
        • 1970-01-01
        • 2011-06-30
        • 1970-01-01
        • 1970-01-01
        • 2014-07-24
        相关资源
        最近更新 更多