[英]Dynamic Form Save into DB
Hello guys hope you can help me and gimme some advice cause i don't know what to do right now. 大家好,希望您能帮助我,给我一些建议,因为我现在不知道该怎么办。 Please kindly help. 请帮助。
I've dynamic form where i can add (append) new row but when i tried to save it's value it said undefined offset. 我有一个动态表单,可以在其中添加(附加)新行,但是当我尝试保存它的值时,它表示未定义的偏移量。 Please look into my code 请查看我的代码
This is my View 这是我的看法
<tbody>
<tr>
<td>1</td>
<td><textarea class="form-control" rows="3" name="analisa[0]" placeholder="Analisa Penyebab" style="resize: none;
height: 50px"></textarea></td>
<td><textarea class="form-control" rows="3" name="tindakan[0]" placeholder="Tindakan Perbaikan dan Pencegahan"
style="resize: none; height: 50px"></textarea></td>
<td><input class="form-control" type="text" name="pic[0]" placeholder="PIC"></td>
<td><input class="form-control" type="date" name="tanggal_pelaksanaan[0]"</td>
</tr>
</tbody>
</table>
<a class="button" href="#" role="button" id="add"> Tambah Analisa</a>
And this is my Javascript 这是我的Javascript
$(document).ready(function(){
var i = 1;
$('#add').click(function(){
i++;
$('#tbanalisa tbody').append("<tr>"+"<td>"+i+"</td>"+"<td><textarea class=\"form-control\" rows=\"3\" name=\"analisa"+i+"\" placeholder=\"Analisa Penyebab\" style=\"resize: none; height: 50px\"></textarea></td>"+"<td><textarea class=\"form-control\" rows=\"3\" name=\"tindakan"+i+"\" placeholder=\"Tindakan Perbaikan dan Pencegahan\"style=\"resize: none; height: 50px\"></textarea></td>"+"<td><input class=\"form-control\" type=\"text\" name=\"pic"+i+"\" placeholder=\"PIC\"></td>"+"<td><input class=\"form-control\" type=\"date\" name=\"tanggal_pelaksanaan"+i+"\" id=\"date"+i+"\" ></td>"+"</tr>");
document.getElementById('count').value = i;
});
});
And this is my function in Controller 这是我在Controller中的功能
for($i=0; $i<2; $i++){
$data[] = array(
'id_tindakan'=>$tindakan->id,
'analisa'=>$request->analisa[$i],
'tindakan'=>$request->tindakan[$i],
'pic'=>$request->pic[$i],
'tanggal_pelaksanaan'=>$request->tanggal_pelaksanaan[$i]
);
}
DB::table('analisas')->insert($data);
I Tried to change index of each array in javascript not analisa[i] but i try analsia[1] and it works fine. 我试图在javascript中更改每个数组的索引,而不是analisa [i],但我尝试使用analsia [1],它工作正常。 Why i make my code error ? 为什么我使我的代码错误?
undefined offset
means either there is nothing at that index or simply your object is not array. undefined offset
意味着该索引处没有任何东西,或者仅仅是对象不是数组。 For your case, I think your variable is not an array. 对于您的情况,我认为您的变量不是数组。 In your Javascript, you do 在您的Javascript中,
name=\"analisa"+i+"\"
Which means the actual name is rendered in html as 这意味着实际名称以html格式呈现为
name="analisa2"
Whereas in Laravel you want to get it with analisa[2]
, which is not an array. 而在Laravel中,您想使用analisa[2]
来获得它,它不是数组。
As a solution, you either change your Javascript to name it as an array 作为解决方案,您可以更改Javascript以将其命名为数组
name=\"analisa["+i+"]\"
Or change your controller to pick the exact same name sent from Javascript 或者更改您的控制器以选择从Javascript发送的完全相同的名称
'analisa'=>$request->analisa.$i,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.