0

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

This is before the PDF

And this is what the PDF looks like

And this is what the PDF looks like

Thank you!.

4
  • When using checkboxes you can use an array input which looks like this, <input type="checkbox" name="fruits[]" value="apple"> <input type="checkbox" name="fruits[]" value="banana">. But I'm not sure if that works for non-checkbox inputs as I'm a bit rusty on my PHP. Despite this, I would try to name your fields more appropriately. Why not use description, rate and price as field names? Commented May 10, 2016 at 22:21
  • If you're writing the html form yourself, why are you naming all the inputs with the same name? Surely the easiest solution is to not do that? Commented May 10, 2016 at 22:21
  • 1
    Rename name attributes. I don't know why would you use same name for different text inputs. Commented May 10, 2016 at 22:23
  • Ok I will try using different name attributes. Thanks :-) Commented May 10, 2016 at 22:28

1 Answer 1

0

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][]"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.