用不同的方法提交两个不同的按钮

时间:2019-01-31 08:56:50

标签: javascript jquery laravel

我正试图在单击按钮1时更新行,而在单击按钮2时创建另一行。

这是更新的控制器

public function update(Request $request, $id){
    $pay = Payroll::find($id);
    $pay ->idnumber = $request->emp_id;
    $pay ->basic = $request->basic;
    $pay ->allowance = $request->allowance;
    $pay ->renderedhours = $request->input('renderedhours');
    $pay ->overtime = $request->overtime;
    $pay ->regular_holiday = $request->regular;
    $pay ->nworking_holiday = $request->nonholiday;
    // $pay ->vacation = $request->editvacation;
    // $pay ->sick = $request->editsick;
    $pay ->undertime = $request->input('undertime');
    $pay ->hdmf = $request->input('hdmf');
    $pay ->late = $request->late;
    $pay ->absent = $request->absent;
    $pay ->sss = $request->sss;
    $pay ->philhealth = $request->philhealth;
    $pay ->holdingtax = $request->holdingtax;
    $pay ->gross = $request->gross;
    $pay ->deduction = $request->deduction;
    $pay ->cut_off = $request->input('cut_off');

    // $pay ->shortdescription = $request->editgrossdeduction;
    $pay ->total = $request->netpay;

    $pay->save();

     return redirect("/payroll");

用于通过更新创建具有相同值的另一行的控制器

public function newpayrolls(Request $request){
    $newpay = new Payroll;

    $newpay ->idnumber = $request->emp_id;
    $newpay ->basic = $request->basic;
    $newpay ->allowance = $request->allowance;
    $newpay ->renderedhours = $request->input('renderedhours');
    $newpay ->overtime = $request->overtime;
    $newpay ->regular_holiday = $request->regular;
    $newpay ->nworking_holiday = $request->nonholiday;
    // $pay ->vacation = $request->editvacation;
    // $pay ->sick = $request->editsick;
    $newpay ->undertime = $request->input('undertime');
    $newpay ->hdmf = $request->input('hdmf');
    $newpay ->late = $request->late;
    $newpay ->absent = $request->absent;
    $newpay ->sss = $request->sss;
    $newpay ->philhealth = $request->philhealth;
    $newpay ->holdingtax = $request->holdingtax;
    $newpay ->gross = $request->gross;
    $newpay ->deduction = $request->deduction;
    $newpay ->cut_off = $request->input('cut_off');

    // $pay ->shortdescription = $request->editgrossdeduction;
    $newpay ->total = $request->netpay;
    $newpay->save();
     return redirect("/payroll");

更新和新工资单的路线

Route::put('update', 'PayrollController@update');
Route::post('/viewpayrolls','PayrollController@newpayrolls')- 
>name('viewpayrolls');

刀片更新并创建新工资单

<form id="my_form" action="/viewpayrolls" method="PUT">
 {{csrf_field()}}

<button class="btn btn-primary" type="submit" value="Submit" value="button1">
        <i class="glyphicon glyphicon-floppy-save small"></i> Save</button>

      <button class ="btn btn-primary btnd" type="submit" value="Submit" value="button2">
      <span class="glyphicon glyphicon-arrow-left small"></span> Save as new Payroll
    </button>

2 个答案:

答案 0 :(得分:0)

最简单的解决方案是使用按钮的ID设置隐藏的输入,并将其与表单一起传递。因此,在单击按钮1时将值设置为“更新”,在单击按钮2时将值设置为“创建”。必须在提交事件触发POST之前完成此操作。

答案 1 :(得分:0)

您可以使用javascrpt来执行此操作,更改表单操作和方法。

$(function () {

    /**
     * change form action 
     */
    $(document).on('click', '#button1', function (e) {
        // e.preventDefault();
        var route = {{ url('update') }};

        $('#my_form').attr('action', route);
        $('#my_form').attr('method', 'put');
    });

    $(document).on('click', '#button2', function (e) {
         // e.preventDefault();
        var route = {{ url('viewpayrolls') }};

        $('#my_form').attr('action', route);
        $('#my_form').attr('method', 'POST');
    });


});