Laravel 5-具有相同名称但保持顺序的多个表单输入 [英] Laravel 5 - Multiple form inputs with the same name but keeping the order

查看:62
本文介绍了Laravel 5-具有相同名称但保持顺序的多个表单输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用L5制作博客系统,并且当前的设置已经准备就绪,除了可以保存博客帖子.

Making a blogging system using L5 and my current set up is all ready except for the saving of the blog posts.

我有2个按钮.一个创建一个textarea输入,另一个创建一个文件上传界面.

I have 2 buttons. One creates a textarea input and the other creates a file upload interface.

基本上,创建博客文章后,我的结构如下:

Essentially, after creating a blog post I am left with the structure like so:

<form>
    <textarea name="text-update">foo</textarea>
    <textarea name="text-update">foo</textarea>
    <textarea name="text-update">foo</textarea>
    <textarea name="text-update">foo</textarea>
    <input type="hidden" value="an image url"/>
    <input type="hidden" value="an image url"/>
    <textarea name="text-update">foo</textarea>
</form>

理想情况下,我希望能够去

Ideally I want to be able to go:

public function store()
{
    foreach (INPUTS AS INPUT) {
        add new row to database with the content and also the type of input.
    }
}

目标是,我没有一个博客,而是拥有属于博客文章的博客部分.

The aim is that instead of having a single blog I instead have blog sections which will belong to a blog post.

如果这不可能,那么我只能增加输入的名称并弄清楚一些东西.

If this isn't possible then Ill just have to increment the names of the inputs and figure something out.

推荐答案

将元素添加到DOM时,您可以使用ID定义数组键,以保留数组顺序.

When you add an element to the DOM you can define the array key with an id to preserve the array order.

您可以通过在名称末尾添加[]来使输入成为数组:

You can make the inputs an array by adding [] at the end of the name:

<form>
    <textarea name="text-update[1]">foo</textarea>
    <textarea name="text-update[2]">foo</textarea>
    <textarea name="text-update[3]">foo</textarea>
    <textarea name="text-update[4]">foo</textarea>
    <input type="hidden" name="image[1]" value="an image url"/>
    <input type="hidden" name="image[2]" value="an image url"/>
    <textarea name="text-update[5]">foo</textarea>
</form>

这会将所有值放入可以迭代的数组中

This will put all the values in an array that you can iterate over

foreach (Request::get('text-update') as $update) {
    //add new row to database with the content and also the type of input.
}
foreach (Request::get('image') as $update) {
    //add new row to database with the content and also the type of input.
}

这篇关于Laravel 5-具有相同名称但保持顺序的多个表单输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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