从boostrap 4 modal提交表单数据问题

时间:2019-06-19 15:46:30

标签: laravel forms bootstrap-modal laravelcollective

我无法从laravel中的模态中获取要提交的数据。当我使用下面的代码并单击“添加用户”按钮时,模态只是关闭,似乎没有在控制器中调用@store方法。数据未提交到数据库

<div class="modal fade" id="addNew" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">New User</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        {!! Form::open(['method' =>'POST', 'action'=> 'UsersController@store', 'files'=>true, 'enctype'=>'multipart/form-data']) !!}
        <div class="form-group {{$errors->has('firstName') ? 'has-error' : ''}}">
          {!! Form::label('firstName', 'First Name:') !!}
          {!! Form::text('firstName', null, ['class'=>'form-control', 'rows' => 3])!!}
          @if($errors->has('firstName'))
            {{$errors->first('firstName')}}
          @endif
        </div>
{{--most form data removed for simplicity--}}
        <div class="form-group">
          {!! Form::submit('Add user', ['class'=>'btn btn-primary']) !!}
        </div>
        {!! Form::close() !!}
      </div>      
    </div>
  </div>
</div>

2 个答案:

答案 0 :(得分:0)

当您的提交按钮作用域超出了表单/ div范围,或者在提交按钮之前使用了其他任何按钮时,会发生这种情况。 由于您没有为简单起见而粘贴其他代码,因此必须在HTML标记之前有一个按钮或该按钮有问题。请检查所有Div标签的格式(打开和关闭),或在此处从源代码添加HTML代码以获取更多帮助。 谢谢, 编码愉快。

答案 1 :(得分:0)

您的表单中缺少

csrf令牌,该令牌不允许在控制器中提交数据。 在表单中添加“ @csrf”,然后重试。