[英]Laravel: How to save on database from dynamic jquery checkbox
今天是个好日子!
我的 Laravel 项目有下一个问题
我有一个动态复选框,我使用 jquery 执行此操作,我的问题是当我尝试保存在我的数据库中时,这将我保存在一行中不同行上选择的所有复选框中的所有值,我使用 implode 保存在单元格中,但在所有单元格上保存相同的结果,“medicamento”和“dosis”的输入文本正常工作,但复选框“cuando1”不起作用。
Jquery 脚本
<script id="document-template" type="text/x-handlebars-template">
<tr class="delete_add_more_item" id="delete_add_more_item">
<td style="width:300px;border: 5px solid transparent"><input type="text" class="form-control" name="medicamentos[]" aria-describedby="medicamentos" id="medicamentos" placeholder="Medicamentos Recetados"></td>
<td style="width:100px;border: 5px solid transparent"><input type="text" class="form-control" name="dosis[]" aria-describedby="dosis" id="dosis" placeholder="Dosis"></td>
<td style="width:50px;border: 5px solid transparent"><label><input type="checkbox" name="cuando[]" id="cuando1" value="Mañana"></label></td>
<td style="width:50px;border: 5px solid transparent"><label><input type="checkbox" name="cuando[]" id="cuando2" value="Tarde"></label></td>
<td style="width:50px;border: 5px solid transparent"><label><input type="checkbox" name="cuando[]" id="cuando3" value="Noche"></label></td>
<td>
<button type="button" class="btn btn-danger"><i class="fa fa-minus fa-2x removeaddmore" style="cursor:pointer;color:white;"></i></button>
</td>
</tr>
</script>
<script type="text/javascript">
$(document).on('click','#addMore',function(){
$('.table').show();
var source = $("#document-template").html();
var template = Handlebars.compile(source);
var data = {
medicamentos: medicamentos,
dosis: dosis,
cuando1: cuando1,
cuando2: cuando2,
cuando3: cuando3,
}
var html = template(data);
$("#addRow").append(html)
});
$(document).on('click','.removeaddmore',function(event){
$(this).closest('.delete_add_more_item').remove();
});
</script>
Controller.php
$number = count($request->medicamentos);
for ($i=0; $i < $number; $i++) {
$cuando1 = implode(', ', (array) $request->get('cuando'));
$Medicamentos = new Medicamentos();
$Medicamentos->idpaciente = $idpaciente;
$Medicamentos->medicamentos = $request->medicamentos[$i];
$Medicamentos->fecha = $hoy;
$Medicamentos->dosis = $request->dosis[$i];
$Medicamentos->cuando1 = $cuando1;
$Medicamentos->save();
}
数据库图像,检查列“cuando1”的所有值
我的刀片带有选中的复选框的图像,看到选中的复选框是保存在数据库中所有行上的内爆。
我想为 aspirina 1:Mañana、Tarde、为 aspirina 2:Mañana 和为 aspirina 3:Noche 存钱
我感谢您的帮助
这是因为您在下面的行中获取了所有复选框的值:
$cuando1 = implode(', ', (array) $request->get('cuando'));
在您的 html 文件中,使用cuando[i][]
而不是cuando[]
] ,其中i
从零开始,并随着模板的每个循环而递增。
然后在您的 controller 中使用:
$cuando1 = implode(', ', (array) $request->cuando[$i]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.