duanraa1984 2016-05-10 22:10
浏览 39
已采纳

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

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

  • 写回答

1条回答 默认 最新

  • duanken7168 2016-05-10 22:39
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?