gpt4 book ai didi

javascript - 从可以动态添加字段的表单将数组存储在数据库中(laravel)

转载 作者:行者123 更新时间:2023-11-28 05:40:47 26 4
gpt4 key购买 nike

我希望在我的注册表中有一个字段,用户可以在其中输入字符串列表。他们应该能够通过单击按钮向列表中添加任意数量的字符串。该列表需要存储在后端并显示在他们的个人资料页面上。

在我的注册 Controller 中,我希望有类似的东西

//set user's list of strings to those from the registration form
$user->items = $request->items

然后在用户个人资料页面上我将显示此元素列表

<ul>
<li>{{$user->item1}}</li>
<li>{{$user->item2}}</li>
<li>{{$user->item3}}</li>
<ul>

最佳答案

html:

 <div id="dynamicInput">
<span class="glyphicon glyphicon-plus" onClick="addInput('dynamicInput');"></span>
</div>

JavaScript:

function addInput(divName)
{
var newdiv = document.createElement('div');
newdiv.innerHTML = "<input type='text' name='items[]'>";
document.getElementById(divName).appendChild(newdiv);
}

存储在后端:

foreach($items as $item)
{
if(!empty($item))
{
$add=new Item(); //Item is the model
$add->name=$item; //saving item to name column
$add->save();
}
}

显示:

$items=Item::where(['id'=>$id])->get(); 
foreach($items as $item)
{
<li>{{$item->name}}</li> // retreiving item from name column
}

关于javascript - 从可以动态添加字段的表单将数组存储在数据库中(laravel),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38939082/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com