After a successful ajax request, I want to send a flash message to my view (For example, upon editing, I'd like to redirect the user to the homepage with $flash = "Your shop has been update" ). Within the controller, it is easy but I don't know what to do within JavaScript. Do any of you know how to figure it out? Im using Laravel
Controller
public function postUpdate (Request $request)
{
$this->validate($request, [
'website_name' => 'required',
'website_url' => 'required',
'category' => 'required',
'type' => 'required',
'sells' => 'required',
'location' => 'required',
'description' => 'required',
'payment' => 'required'
]);
Shop::where('username', '=', Auth::user()->username)->update(['website_name' => Input::get('website_name'),
'website_url' => Input::get('website_url'), 'type' => Input::get('type'), 'category' => Input::get('category'), 'sells' => Input::get('sells'), 'location' => Input::get('location'),
'payment' => Input::get('payment'), 'description' => Input::get('description')]);
return Response::json(['message' => 'Success', 'message_class' => 'alert alert-success fade in']);
}
AJAX
$(".update-form").submit(function(s){
s.preventDefault();
var website_name = $('input[name=website_name]').val();
var website_url = $('input[name=website_url]').val();
var type = $('#type option:selected').val();
var category = $('#category option:selected').val();
var sells = $('input[name=sells]').val();
var location = $('input[name=location]').val();
var payment = $('input[name=payment]').val();
var description = $("textarea#message").val();
$.ajax({
type: "POST",
url: "advertiser/update",
data: {
_token: token, website_name: website_name, website_url: website_url, type: type, category: category, sells: sells, location: location, payment: payment, description: description
},
success: function() {
$('.modal-backdrop').remove();
$('body').load('advertiser')
},
error: function(data) {
$('body').load('advertiser')
}
})
});
HTML
<div class="row" id="errors">
@if (Session::has('message'))
<div class="{!! Session::get('message_class') !!}">
<a href="#" class="close" data-dismiss="alert">×</a>
<strong>Note!</strong> {!! Session::get('message') !!}
</div>
@endif
@if($errors->has())
<div class="alert alert-danger fade in">
<a href="#" class="close" data-dismiss="alert">×</a>
<p>the following errors have occured:</p>
<ul>
@foreach($errors->all() as $error)
<li>{{$error}}</li>
@endforeach
</ul>
</div>
@endif