Skip to content

Instantly share code, notes, and snippets.

@stilian2
Created January 16, 2017 14:53
Show Gist options
  • Save stilian2/782671acfd27fa27e294f5673c5026e0 to your computer and use it in GitHub Desktop.
Save stilian2/782671acfd27fa27e294f5673c5026e0 to your computer and use it in GitHub Desktop.
Meta tag:
<meta name="csrf-token" content="{{ csrf_token() }}">
Link for sending the ajax like button or smth else:
<a href="{{ url('drlresult') }}" class="submit-selection" data-link="{{ url('getAjax') }}" data-token="{{ csrf_token() }}">Radiatorselectie opslaan/toevoegen</a>
ajax setup in the beggining of JS after document ready:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
Ajax it self how it looks like:
$('a.submit-selection').on('click', function(e) {
e.preventDefault();
var url = $(this).attr("data-link");
//add it to your data
var result = {
_token: $(this).data('token'),
productTable: productTable,
accs: accsToProduct,
};
$.ajax({
url: url,
type:"GET",
data: result,
success:function(){
location.href = '../drlresult';
},error:function(){
alert("error!!!!");
}
}); //end of ajax
});
HOW TO SET ROUTE:
Route::get('getAjax/{result?}', 'DrlResultController@getAjax');
HOW TO WORK WITH THE CONTROLLER:
<?php
namespace App\Http\Controllers;
class DrlResultController extends Controller
{
public function drlResults() {
$result = session()->all();
return view('drlresult')
->with('result', $result);
}
public function getAjax(Request $result) {
$productTable = $result['productTable'];
$productAccessoaries = $result['accs'];
session()->push('select', array('product' => $productTable, 'accs' => $productAccessoaries));
;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment