当名称属性相同时,如何使用 PHP 获取多个输入值?

How to get multiple input values with PHP when name attributes are the same?

我想知道如果所有输入都具有相同的名称属性,如何从我的所有输入字段中获取输入值。我也在使用 FPDF 库,我需要在不同的单元格中插入每个值。这是代码...

$pieces = explode(" ", $description);

foreach ($_POST as $key=>$description) {

    if (substr($key,0,6) === "field_"){
        $pdf->Cell(100,10, "$description", 1, 0, "L");
        $pdf->Cell(25,10,"$description",1,0,"C");
        $pdf->Cell(25,10,"$ $description",1,0,"L");
        $pdf->Cell(0,10,"$ $description",1,1,"L");
    }
}

我的HTML如下:

<div class="input_fields_wrap" id="input_fields">
    <div class="new">
        <input class="description" maxlength="255" name="field_0" placeholder="Enter Description" type="text" value="">
        <input class="rate qty" data-rate="rate" maxlength="255" name="field_0" placeholder="0" size="5" type="text" value="">
        <input class="pack price" data-price="price" maxlength="255" name="field_0" placeholder="$ 0.00" size="5" type="text" value="">
        <input class="amount" id="amount" name="field_0" type="text">
    </div>
</div>

正如你在我的 PHP 中看到的,我什至使用了爆炸功能,但我没有得到好的结果。

这是 PDF 之前的内容

这就是 PDF 的样子

谢谢!

您无法使用相同名称 name="field_0" 获取所有输入字段值。而是像这样使输入字段的 name 属性 name="field[0][]".

你的HTML代码应该是这样的:

<div class="input_fields_wrap" id="input_fields">
    <div class="new">
        <input class="description" maxlength="255" name="field[0][]" placeholder="Enter Description" type="text" value="">
        <input class="rate qty" data-rate="rate" maxlength="255" name="field[0][]" placeholder="0" size="5" type="text" value="">
        <input class="pack price" data-price="price" maxlength="255" name="field[0][]" placeholder="$ 0.00" size="5" type="text" value="">
        <input class="amount" id="amount" name="field[0][]" type="text">
    </div>
</div>

这就是您如何处理输入字段值以便在 PDF 文档中显示它们。

$width_array = array(100, 25, 25, 0);
$pos_array = array(0, 0, 0, 1);
$align_array = array('L', 'C', 'L', 'L');
foreach ($_POST['field'][0] as $key => $description) {
    $pdf->Cell($width_array[$key],10, "{$description}", 1, $pos_array[$key], $align_array[$key]);
}

旁注:像这样name="field[0][]",你可能有另一组输入字段,你可以命名它们,name="field[1][]"