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 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP