猿问

如何在laravel中以单一形式处理多个输入?

我有一个表格,我要求用户标记他们的出席情况,我发送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 &nbsp; {{$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>

        </ul>

    </div>

    </form>

</div>


我必须做的是:

  1. 当用户点击“标记”按钮时,该特定字段的值和其余字段应该与空值一起存储,因为所有列都可以为空。

  2. 一旦用户点击按钮并且值成功存储在数据库中,按钮应替换为刻度线按钮,并应禁用到第二天,即午夜。

任何帮助将不胜感激。抱歉,由于未使用堆栈溢出,因此问题格式错误。


紫衣仙女
浏览 132回答 2
2回答

喵喔喔

请试试这个,public function store(Request $request){&nbsp; &nbsp; Timesheet::create([&nbsp; &nbsp; &nbsp; &nbsp; 'in_time' => $request->get('in_time'),&nbsp; &nbsp; &nbsp; &nbsp; 'out_time' => $request->get('out_time'),&nbsp; &nbsp; &nbsp; &nbsp; 'break_out' => $request->get('break_out'),&nbsp; &nbsp; &nbsp; &nbsp; 'break_in' => $request->get('break_in'),&nbsp; &nbsp; &nbsp; &nbsp; 'user_id' => \Auth::user()->id,&nbsp; &nbsp; ]);&nbsp; &nbsp; return redirect()->route('timesheet.create')&nbsp; &nbsp; &nbsp; &nbsp; ->with('success', 'Attendance marked successfully.');}

杨__羊羊

这听起来像是 AJAX 的工作。第1步:为所有按钮添加通用类// new time-btn class<span style="float:right"><button type="submit" class="btn btn-success btn-sm&nbsp;btn time-btn">Mark</button></span>第2步:获取数据并使用 AJAX 发送(使用 jquery):$(document).on('click', '.time-btn', function(e) {&nbsp; &nbsp;e.preventDefault();&nbsp; &nbsp;// get value of first input before the button&nbsp; &nbsp;var time = $(this).prev('input').val();&nbsp; &nbsp;// get input name&nbsp; &nbsp;var name = $(this).prev('input').attr('name')&nbsp; &nbsp;// validate time if necessary&nbsp; &nbsp; &nbsp;$.ajax({&nbsp; &nbsp; &nbsp; &nbsp; url: '/time/store', // insert route url here&nbsp; &nbsp; &nbsp; &nbsp; type: 'POST',&nbsp; &nbsp; &nbsp; &nbsp; data: {time: time, name: name},&nbsp; &nbsp; &nbsp; &nbsp; success: function( response ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response = JSON.parse( response );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// add tick mark to button here&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; error: function (xhr, status, error) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log( xhr.responseText );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;});&nbsp; });第 3 步:更新控制器public function store(Request $request){&nbsp; &nbsp;Timesheet::create([&nbsp; &nbsp; 'in_time' => request('name') == 'in_time' ? request('time') : null,&nbsp; &nbsp; 'out_time' => request('name') == 'out_time' ? request('time') : null,&nbsp; &nbsp; // do the same for tother&nbsp; &nbsp; ...&nbsp; &nbsp; 'user_id' => Auth::user()->id,]);return redirect()->route('timesheet.create')&nbsp; &nbsp; ->with('success', 'Attendance marked successfully.');}
随时随地看视频慕课网APP
我要回答