在 Bootstrap Modal 中保存 Laravel 表单中的数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23729730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:32:16  来源:igfitidea点击:

Saving Data from Laravel Form in Bootstrap Modal

formstwitter-bootstraplaravelmodal-dialog

提问by randytan

i have a bootstrap modal dialog which use laravel form to register a user.

我有一个引导模式对话框,它使用 laravel 表单来注册用户。

Here's the code:

这是代码:

<div id="addPenggunaModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="ModalLabel" aria-hidden="true">
<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="ModalLabel">Tambah Pengguna Baru</h3>
</div>
<div class="modal-body">
    {{ Form::open(array('url'=>'users/addpengguna','class'=>'form-horizontal', 'method'=> 'POST')) }}
    <ul>
        @foreach($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
    </ul>

    <div class="control-group">
        <label for="firstname" class="control-label">First Name:</label>
        <div class="controls">
            {{ Form::text('firstname', null, array('class'=>'span3', 'placeholder'=>'First Name')) }}
        </div>
    </div> <!-- /field -->

    <div class="control-group">
        <label for="lastname" class="control-label">Last Name: </label>
        <div class="controls">
            {{ Form::text('lastname', null, array('class'=>'span3', 'placeholder'=>'Last Name')) }}
        </div>
    </div> <!-- /field -->


    <div class="control-group">
        <label for="email" class="control-label">Email Address: </label>
        <div class="controls">
            {{ Form::text('email', null, array('class'=>'span3', 'placeholder'=>'Email Address')) }}
        </div>
    </div> <!-- /field -->

   <div class="control-group">
        <label for="password" class="control-label">Password:</label>
        <div class="controls">
            {{ Form::password('password', array('class'=>'span3', 'placeholder'=>'Password')) }}
        </div>
    </div> <!-- /field -->

    <div class="control-group">
        <label for="confirm_password" class="control-label">Confirm Password:</label>
        <div class="controls">
            {{ Form::password('password_confirmation', array('class'=>'span3', 'placeholder'=>'Confirm Password')) }}
        </div>    
    </div> <!-- /field -->

    <div class="control-group">
        <label for="type_user" class="control-label">Tipe Pengguna:</label>
        <div class="controls">
            {{ Form::radio('level', '1');  }} Supervisor
            {{ Form::radio('level', '0');  }} Sales 
        </div>    
    </div> <!-- /field -->


    </form>
</div>
<div class="modal-footer">
    {{ Form::submit('Simpan', array('class'=>'button btn btn-primary','id'=>'mdl_save_change'))}}
    <button class="btn" data-dismiss="modal" aria-hidden="true">Batal</button>
</div>
{{ Form::close() }}
</div>

then i use the controller to save the details:

然后我使用控制器来保存详细信息:

public function postAddpengguna(){
 /* function to add user in data pengguna */
    $validator = Validator::make(Input::all(), User::$rules);
    if($validator -> passes()){
        $user = new User;
        $user->firstname = Input::get('firstname');
        $user->lastname = Input::get('lastname');
        $user->email = Input::get('email');
        $user->password = Hash::make(Input::get('password'));
        $user->level = Input::get('level');
        /* save the following details */
        $user->save();
        return Redirect::to('pengguna');
    } else {
        return Redirect::to('index');
    }
}

but the form doesn't save any data to database. I have another page called registration and it works.

但表单不会将任何数据保存到数据库中。我有另一个名为注册的页面,它可以工作。

my questions:

我的问题:

  1. how to trace POST from laravel form submit, is there any browser extension?
  2. how to trace error log in laravel
  3. any ideas what's going on in my problem?
  1. 如何从laravel表单提交跟踪POST,是否有任何浏览器扩展?
  2. 如何在laravel中跟踪错误日志
  3. 任何想法我的问题发生了什么?

thank you in advance.

先感谢您。

UPDATE

更新

Here's the screenshot that describe how this works.

这是描述其工作原理的屏幕截图。

bootstrap modal:

引导模式:

bootstrap modal

引导模式

when i press the submit button (blue button in modal) i want it to save the data to db. The function php is shown above.

当我按下提交按钮(模式中的蓝色按钮)时,我希望它将数据保存到 db。php 函数如上所示。

PS. i don't use any AJAX to call the value from the FORM. But when i use the AJAX, it always error because TOKEN is missing.

附注。我不使用任何 AJAX 来调用 FORM 中的值。但是当我使用 AJAX 时,它总是出错,因为缺少 TOKEN。

回答by Pham Hoang Nam

You may use this code with your ajax code:

您可以将此代码与您的 ajax 代码一起使用:

$(function() {
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN':  {!! json_encode(csrf_token()) !!}
        }
    });
});

回答by Mahbub

First of all check the actionand _tokenfield of form . To add token field in your form you should include the following line in your form:

首先检查表单的action_token字段。要在表单中添加令牌字段,您应该在表单中包含以下行:

<input type="hidden" name="_token" value="{{csrf_token()}}">

<input type="hidden" name="_token" value="{{csrf_token()}}">

To re-use bootstrap modal in your project you can check this Githublink

要在您的项目中重新使用引导模式,您可以查看此Github链接

回答by phaberest

In the latest versions of laravel 5 you can use a shortcut to get the token field.

在最新版本的 laravel 5 中,您可以使用快捷方式来获取令牌字段。

<form ... >
  {!! csrf_field() !!}
</form>

In this case you'll get something like

在这种情况下,你会得到类似的东西

<input type="hidden" name="_token" value="hpyL7cUbCMFBGRfCi2dpzE5XHGj8WuyY2jqloKRx">

You can in any case get the token string calling csrf_token(), anyway I honestly prefer the csrf_field()alternative.

在任何情况下,您都可以获取令牌字符串调用csrf_token(),无论如何,老实说,我更喜欢csrf_field()另一种选择。