使用Ajax和Laravel保存数据

时间:2019-04-25 06:21:58

标签: php ajax laravel

我只有一个输入表单,我想使用Ajax,PHP和Laravel保存到我的数据库中,此代码对我的其他模块很好。但是我不能仅仅找到错误的原因,为什么这段代码无法正常工作。帮助我找出我的代码有什么问题。 Route是正确的,我的控制器似乎也正确,但是我真的怀疑我的Ajax是否正确。

HTML:

  <input id="schedDate" name = "schedDate" type="text" class="datetimepicker-input form__field" placeholder="Schedule Date" data-target="#schedDate" data-toggle="datetimepicker">

  <input id="timeIn" name = "timeIn" type="text" class="datetimepicker-input form__field" placeholder="Time In" data-target="#timeIn" data-toggle="datetimepicker">

  <input id="timeOut" name = "timeOut" type="text" class="datetimepicker-input form__field" placeholder="Time Out" data-target="#timeOut" data-toggle="datetimepicker">

   <select id="cmbShift" name = "cmbShift" class="form__field" placeholder="Shift Type">
       <option value="">- Select Shift -</option>
       <option value="Pre-Shift">Pre-Shift</option>
       <option value="Post-Shift">Post-Shift</option>
   </select>

    <textarea type="text" id="txtReason" name = "txtReason" class="form__field" placeholder="Reason"></textarea>
    <input id="btnApplyAlter" name="btnApplyAlter" class="btnApplyAlter btn btn-sm button blue pull-right" type="button" value="Apply Overtime" style="width:220px;"/>

这是我的Ajax:

$(document).on("click", ".btnApplyAlter", function(){

var c = confirm("Apply this Overtime?");
var schedDate = $("#schedDate").val();
var timeIn = $("#timeIn").val();
var timeOut = $("#timeOut").val();
var cmbShift = $("#cmbShift").val();
var txtReason = $("#txtReason").val();

if(c == true)
{
    $.ajax({
        headers:{'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
        url: "{{ route('saveovertime') }}",
        method: "POST",
        data: {sched_date:schedDate,time_in:timeIn,time_out:timeOut,cmbShift:cmbShift,txtReason:txtReason}, 
        dataType: "json",
        success:function(data)
        {
            alert(data);
            refresh_Table();
        },
        error: function(xhr, ajaxOptions, thrownError){
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    }); 
}
 });

这是我的控制器:

    public function save_overtime(Request $request){
    //Code for getting the current date of Asia/Manila
    date_default_timezone_set('Asia/Manila');
    $todays_date = date("Y-m-d H:i:s");
    $today = strtotime($todays_date);
    $todayDate = date("Y-m-d H:i:s", $today); 
    //Code for getting the current date of Asia/Manila

    $apply_overtime = new OvertimeRecords;
    $apply_overtime->company_id = auth()->user()->company_id;
    $apply_overtime->date_applied = $todayDate;
    $apply_overtime->sched_date = $request->sched_date;
    $apply_overtime->shift_applied = $request->cmbShift;
    $apply_overtime->date_timein = $request->time_in;
    $apply_overtime->date_timeout = $request->time_out;
    $apply_overtime->reason = $request->txtReason;
    $apply_overtime->lu_by = auth()->user()->name; 
    $apply_overtime->save();
    $message = "Overtime Applied Succesfully!"; 
    echo json_encode($message);
}

这是我的路线:

 Route::post('overtimerecords/saveovertime', 'UsersController\\OvertimeRecordsController@save_overtime')->name('saveovertime');

错误: enter image description here

此错误直接突出显示为ajax

enter image description here

1 个答案:

答案 0 :(得分:0)

首先放置jquery,然后在标题中放置其他js。它的原因是jquery似乎在bootstrap js之后出现。