Ajax - Laravel 5.2 Auth

时间:2016-09-01 06:35:46

标签: php jquery ajax laravel laravel-5.2

我想通过ajax发送表单。我阅读了一些教程,但我没有找到解决方案。

我使用"创建此CRUD; php artisan make:auth"

控制器

protected function create(Request $request){ 
        $users = new User;
        $users->nume = $request['nume'];
        $users->prenume = $request['prenume'];
        $users->cnp = $request['cnp'];
        $users->mobil = $request['mobil'];
        $users->email = $request['email'];
        $users->password = bcrypt($request['password']);
        $users->save();
        return Response::json();   
}

路线

Route::post('/register',array(
   'as' => 'create',
    'uses' => 'Auth\AuthController@create'
));

Jquery的

jQuery( document ).ready( function( $ ) {

$( '#continua1' ).on( 'submit', function() {

    //.....
    //show some spinner etc to indicate operation in progress
    //.....

    $.post(
            $( this ).prop( 'action' ),
            {
                "_token": $( this ).find( 'input[name=_token]' ).val(),
                "$users->nume": $( '#nume' ).val(),
                "$users->prenume": $( '#prenume' ).val(),
                "$users->cnp": $( '#cnp' ).val(),
                "$users->mobil": $( '#mobil' ).val(),
                "$users->email": $( '#email' ).val(),
                "$users->password": $( '#password' ).val(),

            },
            function( data ) {
                //do something with data/response returned by server
            },
            'json'
    );

    //.....
    //do anything else you might want to do
    //.....

    //prevent the form from actually submitting in browser
    return false;
});

});

FORM

<form class="form-horizontal" role="form" method="POST" action="{{ url('/register') }}">
                        {{ csrf_field() }}

                        <div class="form-group{{ $errors->has('nume') ? ' has-error' : '' }}">
                            <label for="nume" class="col-md-4 control-label">Nume</label>

                            <div class="col-md-6">
                                <input required="required" id="nume" type="text" class="form-control" name="nume" value="{{ old('nume') }}">

                                @if ($errors->has('nume'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('nume') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group{{ $errors->has('prenume') ? ' has-error' : '' }}">
                            <label for="prenume" class="col-md-4 control-label">Prenume</label>

                            <div class="col-md-6">
                                <input id="prenume" type="text" class="form-control" name="prenume" value="{{ old('prenume') }}">

                                @if ($errors->has('prenume'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('prenume') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group{{ $errors->has('cnp') ? ' has-error' : '' }}">
                            <label for="cnp" class="col-md-4 control-label">CNP</label>

                            <div class="col-md-6">
                                <input id="cnp" type="text" class="form-control" name="cnp" value="{{ old('cnp') }}">

                                @if ($errors->has('cnp'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('cnp') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group{{ $errors->has('mobil') ? ' has-error' : '' }}">
                            <label for="mobil" class="col-md-4 control-label">Mobil</label>

                            <div class="col-md-6">
                                <input id="mobil" type="text" class="form-control" name="mobil" value="{{ old('mobil') }}">

                                @if ($errors->has('mobil'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('mobil') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                            <label for="email" class="col-md-4 control-label">E-Mail Address</label>

                            <div class="col-md-6">
                                <input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}">

                                @if ($errors->has('email'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('email') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
                            <label for="password" class="col-md-4 control-label">Password</label>

                            <div class="col-md-6">
                                <input id="password" type="password" class="form-control" name="password">

                                @if ($errors->has('password'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('password') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group{{ $errors->has('password_confirmation') ? ' has-error' : '' }}">
                            <label for="password-confirm" class="col-md-4 control-label">Confirm Password</label>

                            <div class="col-md-6">
                                <input id="password-confirm" type="password" class="form-control" name="password_confirmation">

                                @if ($errors->has('password_confirmation'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('password_confirmation') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">
                                <button type="button" id="continua1" class="btn btn-primary">
                                    <i class="fa fa-btn fa-user"></i> Continua
                                </button>
                            </div>
                        </div>
                    </form>

2 个答案:

答案 0 :(得分:0)

试试这个:

JS:

$.ajax({
    url         : '{{ url("/createuser") }}',
    method      : 'post',
    headers:
    {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    },
    data        :
    {
        variable1   : value,   // variable and its values
    },
    success     : function(response)
    {
        // do your stuff on success
    }
});

路线:

Route::post('/createuser',[
    'middleware'=>'auth',
    'uses'=>'UserManagementController@createUser'
]);

控制器:

public function createUser()
{
    // do your stuff here
}

答案 1 :(得分:0)

您可以使用serialize();

获取所有输入

Ajax和jquery:

$(document).on('submit','#formid',function(e)
{
    e.preventDefault();
    var form = $(this);
    $.ajax({
        type: form.attr('method'),
        url: form.attr('action'),
        data: form.serialize(),
        success: function (data)
            {
                 alert('ajax requet success!');
             }
      });
});

和控制器:

protected function create(Request $request)
    {
      if($request->ajax())
      {
        $users = new User;
        $users->nume = $request['nume'];
        $users->prenume = $request['prenume'];
        $users->cnp = $request['cnp'];
        $users->mobil = $request['mobil'];
        $users->email = $request['email'];
        $users->password = bcrypt($request['password']);
        $users->save();
        return Response::json();
      }
    }