dimanche 27 septembre 2015

Laravel Ajax posting data into the database

This is my blade.php =>

Drink A Coffee todayCoffee OrdersAdd a Coffee Order

{!! Form::open(['url' => 'index', 'method' => 'POST']) !!}Name:

Drink:

Add!

{!! Form::close() !!}This is my controller => public function makeOrder() { if (Request::ajax()) { $post = Request::all(); $order = new Order; $order->name = $post['name']; $order->drink = $post['drink']; $order->save(); return redirect()->back(); $response = array( 'status' => 'success', 'msg' => 'Option created successfully', ); return Response::json( $response ); }this is my scripts=> $(document).ready(function() { $('#add-order').click(function(e) { e. preventDefault(); //setting variables based on the input fields var inputName = $('input[name="name"]').val(); var inputDrink = $('input[name="drink"]').val(); var token = $('input[name="_token"]').val(); var data = {name:inputName, drink:inputDrink, token:token}; var request = $.ajax({ url: "index", type: "POST", data: data, dataType:"html" }); request.done(function( msg ) { var response = JSON.parse(msg); console.log(response.msg); }); request.fail(function( jqXHR, textStatus ) { console.log( "Request failed: " + textStatus ); }); });}); But anytime i submit my form and i the 'inspect element', i see this error "Failed to load resource: the server responds with a status of 500 (Internal Server Error)"

via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire