display flash and error message after ajax request Laravel

Juan Carlo F. Yarra

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">&times;</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">&times;</a>

                    <p>the following errors have occured:</p>
                    <ul>
                        @foreach($errors->all() as $error)
                            <li>{{$error}}</li>
                        @endforeach
                    </ul>
                </div>

            @endif
Sulthan Allaudeen

You can't redirect the within controller if you use ajax request.

But you can do like this.

Send some parameters in the Controller like this

$Response   = array(
            'success' => '1',
        );

or

$Response   = array(
            'success' => '0',
            'error' => 'Your Flash Message'
        );

and return it return $Response;

Then, In the ajax result you can redirect the user like this

if (data.success == 1){
    window.location = 'toyourdesiredpath';
}
else
{
//show your error message in your div or span
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

laravel how to display flash message with confirm message

From Dev

Laravel 5.2 & AJAX - Display success message after redirect

From Dev

How to display a flash message after multiple redirects?

From Dev

Setting display time of flash message in laravel 4.2

From Dev

Setting display time of flash message in laravel 4.2

From Dev

Laravel 5.2: Display a session flash message after user login/logout and registration

From Dev

Laravel flash message showing after refresh

From Dev

Flash message display grails

From Dev

Laravel flash message stays on page for more than one request

From Dev

Rails doesn't display flash messages after ajax call

From Dev

parsley display error message from ajax response

From Dev

parsley display error message from ajax response

From Dev

display error message after submitting a form

From Dev

'Junk after document element' error in AJAX request

From Dev

Error: CORS in laravel/VueJS ajax request

From Dev

Laravel Ajax Request returns error 500

From Dev

Flash message not working in laravel 5.2

From Dev

Not able to display message after submitting form using ajax

From Dev

flask flash message doesn't show up after request sent by javascript

From Dev

Flash Message Error in Rails app?

From Dev

Can't display json result after ajax request on Mamp System

From Dev

Display error message when ajax result return null value

From Dev

Laravel array validation via ajax : showing error message throws error

From Dev

Laravel 5 flash message flashes again after navigating away and using back button

From Dev

POST request error in Ajax. Blank page after error

From Dev

Unable to display flash message in view with ejs template

From Dev

Display different Devise flash message on first login

From Dev

How to display flash message on login with passport ldapauth

From Dev

how to display flash message when layout false

Related Related

  1. 1

    laravel how to display flash message with confirm message

  2. 2

    Laravel 5.2 & AJAX - Display success message after redirect

  3. 3

    How to display a flash message after multiple redirects?

  4. 4

    Setting display time of flash message in laravel 4.2

  5. 5

    Setting display time of flash message in laravel 4.2

  6. 6

    Laravel 5.2: Display a session flash message after user login/logout and registration

  7. 7

    Laravel flash message showing after refresh

  8. 8

    Flash message display grails

  9. 9

    Laravel flash message stays on page for more than one request

  10. 10

    Rails doesn't display flash messages after ajax call

  11. 11

    parsley display error message from ajax response

  12. 12

    parsley display error message from ajax response

  13. 13

    display error message after submitting a form

  14. 14

    'Junk after document element' error in AJAX request

  15. 15

    Error: CORS in laravel/VueJS ajax request

  16. 16

    Laravel Ajax Request returns error 500

  17. 17

    Flash message not working in laravel 5.2

  18. 18

    Not able to display message after submitting form using ajax

  19. 19

    flask flash message doesn't show up after request sent by javascript

  20. 20

    Flash Message Error in Rails app?

  21. 21

    Can't display json result after ajax request on Mamp System

  22. 22

    Display error message when ajax result return null value

  23. 23

    Laravel array validation via ajax : showing error message throws error

  24. 24

    Laravel 5 flash message flashes again after navigating away and using back button

  25. 25

    POST request error in Ajax. Blank page after error

  26. 26

    Unable to display flash message in view with ejs template

  27. 27

    Display different Devise flash message on first login

  28. 28

    How to display flash message on login with passport ldapauth

  29. 29

    how to display flash message when layout false

HotTag

Archive