通过在laravel中快速单击来防止多次提交表单

Dan*_*cia 1 php forms laravel

因此,我遇到了一个问题,即如果我单击足够快的提交按钮,我的表单将被提交几次。我该如何预防?令牌是自动添加的,但我猜对它没有帮助。表格范例:

  <div class="row padding-10">
    {!! Form::open(array('class' => 'form-horizontal margin-top-10')) !!}
    <div class="form-group">
      {!! Form::label('title', 'Title', ['class' => 'col-md-1 control-label padding-right-10']) !!}
      <div class="col-md-offset-0 col-md-11">
      {!! Form::text('title', null, ['class' => 'form-control']) !!}
      </div>
    </div>
    <div class="form-group">
      {!! Form::label('body', 'Body', ['class' => 'col-md-1 control-label padding-right-10']) !!}
      <div class="col-md-offset-0 col-md-11">
      {!! Form::textarea('body', null, ['class' => 'form-control']) !!}
      </div>
    </div>
    <div class="col-md-offset-5 col-md-3">
      {!! Form::submit('Submit News', ['class' => 'btn btn-primary form-control']) !!}
    </div>
    {!! Form::close() !!}
  </div>
Run Code Online (Sandbox Code Playgroud)

我的NewsController存储方法:

    public function store()
{
$validator = Validator::make($data = Input::all(), array(
  'title' => 'required|min:8',
  'body' => 'required|min:8',
));

if ($validator->fails())
{
  return Redirect::back()->withErrors($validator)->withInput();
}

News::create($data);

return Redirect::to('/news');
}
Run Code Online (Sandbox Code Playgroud)

sno*_*FFF 5

一种方法是对按钮使用单击处理程序,该处理程序首先禁用按钮,然后提交表单。

<script>
    function submitForm(btn) {
        // disable the button
        btn.disabled = true;
        // submit the form    
        btn.form.submit();
    }
</script>

<input id="submitButton" type="button" value="Submit" onclick="submitForm(this);" />
Run Code Online (Sandbox Code Playgroud)