gpt4 book ai didi

javascript - 保存用户动态添加的字段中的多个输入

转载 作者:行者123 更新时间:2023-12-02 22:43:24 24 4
gpt4 key购买 nike

我有一个简单的表单,用户可以根据需要添加多个表单,我使用 laravel 和 ajax。

这是 jsfiddle: dynamic adding field by user

这是 HTML

<form id="paramsForms">
{{csrf_field()}}


<div class="modal-body">
<div class="container h-100">
<div class="d-flex justify-content-center">

<div class="card mt-5 col-md-12 animated bounceInDown myForm" id="multiple-container">
<div class="card-header">
<h4>Bidders Information</h4>
</div>
<div class="card-body" id="add_info">
<div id="dynamic_container">

<small id="bidder">Bidder 1</small>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text br-15"><i class="fa fa-tags"></i></span>
</div>
<input type="text" placeholder="Bidders Name" name="bidders_name[]" class="form-control"/>
</div>
<div class="input-group mt-3">
<div class="input-group-prepend">
<span class="input-group-text br-15"><i class="fa fa-tags"></i></span>
</div>
<input type="text" placeholder="atribute name" name="params_name[]" class="form-control"/>
<input type="number" placeholder="atribute value" name="params_value[]" class="form-control"/>
<a class="btn btn-secondary btn-sm moreinput_field" id="add_more_input">
<i class="fa fa-plus-circle"></i>
</a>
</div>
</div>
</div>

<div class="card-footer" id="card-footer">
<a class="btn btn-success btn-sm" id="add_more"><i class="fa fa-plus-circle"></i> Add</a>
<!-- <button class="btn btn-success btn-sm float-right submit_btn"><i class="fas fa-arrow-alt-circle-right"></i> Submit</button> -->
</div>

</div>


</div>
</dvi>
</div>


<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" id="save" class="btn btn-primary">Save changes</button>
</div>

</form>

这是我 Controller 中的商店功能

public function store(Request $request){
dd($request);

if($request->ajax())
{
$rules = array(
'params_name.*' => 'required',
'params_value.*' => 'required',
'bidders_name.*' => 'required'
);
$error = Validator::make($request->all(), $rules);
if($error->fails())
{
return response()->json([
'error' => $error->errors()->all()
]);
}

$params_name = $request->params_name;
$params_value =$request->params_value;
$bidders_name =$request->bidders_name;

for($count = 0; $count < count($params_name); $count++)
{
$data = array(
'params_name' => $params_name[$count],
'params_value' => $params_value[$count],
'bidders_name' => $bidders_name[$count],

);
$insert_data[] = $data;
}

Parameter::store($insert_data);
return response()->json([
'success' => 'Data Added successfully.'
]);
}

}

这是我的ajax,无需重新加载即可保存数据

  $("#paramsForms").on('submit', function(e) {
e.preventDefault();

$.ajax({
url: '/parameters',
type: "POST",
data: $(this).serialize(),
dataType: 'json',
beforeSend:function() {
$("#save").attr('disabled', 'disabled');
},
success:function (data) {
console.log(data);
alert('Data successfull saved');
},
error:function (error) {
console.log(error)
console.log('Data not saved');
}
})
})

现在,当用户添加例如三组输入并提交表单时,我收到消息数据已成功保存,但是当我检查数据库时,只有第一组输入被保存,当我添加dd($request) 我得到以下内容

enter image description here

我的代码做错了什么?

最佳答案

您正在将新卡片包装在表单元素中。

41 var html =`  <div class="card-body" id="add_info${i}">
42 <form> //remove me
...
63 </form> //remove me
64 </div>`;

关于javascript - 保存用户动态添加的字段中的多个输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58512703/

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