jeudi 4 août 2016

Ajax submit form Laravel 5.1

Hello guy's i try to figure out a Live feed application in Laravel 5.1 when user can submit their posts. I'm still in stuck with Ajax because i want to save data in DB usign Ajax request.

Here is my form

{!! Form::open(array('url'=>'saveposts','method'=>'POST')) !!}                     
                     <div class="panel-body">
                        <textarea name="post" class="form-control share-text" rows="3" placeholder="Share your status..."></textarea>
                    </div>
                    <div class="panel-footer share-buttons">
                      <a href="#"><i class="fa fa-map-marker"></i></a>
                      <a href="#"><i class="fa fa-photo"></i></a>
                      <a href="#"><i class="fa fa-video-camera"></i></a>
                     <button type="submit" class="send-btn btn btn-primary btn-xs pull-right display-none" id="submit">Post</button> 
                    {!! Form::close() !!}

Here is Ajax script

<script type="text/javascript">
$(document).ready(function(){
  $('#submit').click(function(){            
    $.ajax({
      url: 'saveposts',
      type: "post",
      data: {'post':$('input[name=post]').val(), '_token': $('input[name=_token]').val()},
      success: function(data){
        alert(data);
      }
    });      
  }); 
});
</script>

The route:

Route::post('saveposts', 'UserController@savePosts');

And the method in Controller

public function savePosts() {
    if(Requestjx::ajax()) {
        $data = Input::all();
        return Response::json([
                'error' => false,
                'insertedData' => $data
            ], 200);
    }
}

What' is wrong? It didn't work. Any help is appreciated. Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire