从具有相同名称的多个输入的表单中请求数据 [英] Request data from form with multiple inputs using the same name

查看:85
本文介绍了从具有相同名称的多个输入的表单中请求数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我创建了一个表单,该表单将提交到两个单独的表.这是用于发票创建的.每个发票都有可变数量的行,因此我创建了两个表,一个用于发票,一个用于每一行.这些行通过外键绑定到发票表.

So I've created a form that that will submit to two separate tables. It's for invoice building. Each invoice has a variable amount of rows, so I've created two tables, one for the invoice and one for each row. The rows are tied to the invoice table via a foreign key.

使用纯PHP,只需将输入命名为"name []",然后遍历它们即可.但是在Laravel中,这似乎行不通.基本上,我有:

With pure PHP it's simply a matter of naming the inputs "name[]" and then looping through them. But in Laravel, it's not seeming to work. Basically, I have:

$invoice = new Invoice;
$invoice->fill($data);
$invoice->save();
for ($i = 0; $i < count($request->item); $i++){
    $row = new InvoiceRow;
    $row->fill(array('item' => $request->item[$i], 'description' => $request->description[$i], 'cost' => $request->cost[$i], 'quantity' => $request->quantity[$i], 'total' => $request->itemTotal[$i]));
    $invoice->InvoiceRows()->save($row);
}

它将保存第一行,但不保存其他行.

It will save the first row, but not the other rows.

这是一些标记,没有用于引导和验证的类.该行的每一行都将使用jQuery克隆.

Here is some of the markup, without the classes for bootstrap and validation. The row is being cloned with jQuery for each neccessary line for the document.

<tr class="item-row">
                        <td> class="form-control form-inline item-name" name="item[]">
                        </td>
                        <td>
                            <input type="text" name="description[]">
                        </td>
                        <td>
                            <input type="number"  name="cost[]">
                        </td>
                        <td>

                                <input name="quantity[]">

                        </td>
                        <td>
                            <input type="number" name="itemTotal[]">
                        </td>
                    </tr>

推荐答案

迭代该对象,例如

$name = $request->name; //here name is the name of your form's name[] field

foreach($name as $names)
{
//do something
}

这篇关于从具有相同名称的多个输入的表单中请求数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆