简体   繁体   中英

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

I would like to know how to get the input value from all my input fields if all inputs have the same name attribute. I'm also using the FPDF library and I need to insert every value in different cells. This is the code...

$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");
    }
}

My HTML is the following:

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

As you can see in my PHP I even used the explode function but I don't get good results.

This is before the PDF

这是在PDF之前

And this is what the PDF looks like

这就是PDF的样子

Thank you!.

You can't get all the input fields values using the same name name="field_0" . Instead make the name attribute of input fields like this, name="field[0][]" .

Your HTML code should be like this:

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

And this is how you can process the input field values in order to display them in PDF document.

$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]);
}

Sidenote: Like this name="field[0][]" , you might have another set of input fields which you can name them, name="field[1][]"

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