'Saving Data from Laravel Form in Bootstrap Modal

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

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?

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.

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.



Solution 1:[1]

First of all check the action and _token field of form . To add token field in your form you should include the following line in your form:

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

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

Solution 2:[2]

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

<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.

Solution 3:[3]

You may use this code with your ajax code:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mahbub
Solution 2 phaberest
Solution 3 bdkopen