我有一个表格,要求用户标记他们的出席情况,我将Carbon::now()
作为默认输入和隐藏输入发送,每当用户单击按钮时,它都应该存储在数据库中,但是问题是我有四个这样的字段,每当我点击“提交”按钮时,它都会存储该字段的所有值。我是这个东西的新手,请帮助我。
我也包括代码。
查看:
<div class="card" style="max-width: 600px; margin:auto; margin-bottom:50px;margin-top:40px;">
<div class="card-header" style="text-align:center">
Today: @php $today = \Carbon\Carbon::now()->format('d-m-Y'); @endphp {{$today}}
</div>
<form action="{{ route('timesheet.store') }}" method="POST">
@csrf
<div class="card-body">
@php
$now = \carbon\Carbon::now();
@endphp
<ul class="list-group">
<li class="list-group-item">
<label for="in_time">Day start</label>
<input name="in_time" type="datetime" style="display:none" value="{{$now}}">
<span style="float:right"><button type="submit" class="btn btn-success btn-sm">Mark</button></span>
</li>
<li class="list-group-item">
<label for="break_out">Break-out</label>
<input name="break_out" type="datetime" style="display:none" value="{{$now}}">
<span style="float:right"><button type="submit" class="btn btn-success btn-sm">Mark</button></span>
</li>
<li class="list-group-item">
<label for="break_in">Break-in</label>
<input name="break_in" type="datetime" style="display:none" value="{{$now}}">
<span style="float:right"><button type="submit" class="btn btn-success btn-sm">Mark</button></span>
</li>
<li class="list-group-item">
<label for="out_time">Day end</label>
<input name="out_time" type="datetime" style="display:none" value="{{$now}}">
<span style="float:right"><button type="submit" class="btn btn-success btn-sm">Mark</button></span>
</li>
</ul>
</div>
</form>
</div>
控制器:
public function store(Request $request)
{
Timesheet::create([
'in_time' => request('in_time'),
'out_time' => request('out_time'),
'break_out' => request('break_out'),
'break_in' => request('break_in'),
'user_id' => Auth::user()->id,
]);
return redirect()->route('timesheet.create')
->with('success', 'Attendance marked successfully.');
}
我要做的是:
任何帮助将不胜感激。 抱歉,问题的格式错误,因为尚未使用堆栈溢出。
答案 0 :(得分:0)
这听起来像是AJAX的工作。
第1步:
将通用类添加到所有按钮
// new time-btn class
<span style="float:right"><button type="submit" class="btn btn-success btn-sm
btn time-btn">Mark</button></span>
第2步:
获取数据并使用AJAX发送(使用jquery):
$(document).on('click', '.time-btn', function(e) {
e.preventDefault();
// get value of first input before the button
var time = $(this).prev('input').val();
// get input name
var name = $(this).prev('input').attr('name')
// validate time if necessary
$.ajax({
url: '/time/store', // insert route url here
type: 'POST',
data: {time: time, name: name},
success: function( response ) {
response = JSON.parse( response );
// add tick mark to button here
},
error: function (xhr, status, error) {
console.log( xhr.responseText );
}
});
});
第3步:
更新控制器
public function store(Request $request)
{
Timesheet::create([
'in_time' => request('name') == 'in_time' ? request('time') : null,
'out_time' => request('name') == 'out_time' ? request('time') : null,
// do the same for tother
...
'user_id' => Auth::user()->id,
]);
return redirect()->route('timesheet.create')
->with('success', 'Attendance marked successfully.');
}
答案 1 :(得分:0)
请尝试一下
public function store(Request $request)
{
Timesheet::create([
'in_time' => $request->get('in_time'),
'out_time' => $request->get('out_time'),
'break_out' => $request->get('break_out'),
'break_in' => $request->get('break_in'),
'user_id' => \Auth::user()->id,
]);
return redirect()->route('timesheet.create')
->with('success', 'Attendance marked successfully.');
}