Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh/Reload page after Ajax Sucess - Laravel

Tags:

ajax

php

laravel

In my laravel project, i want to refresh my page after an ajax success but my page would refresh after the success. I tried to refresh with laravel redirect in the controller and it didn't work, i have also tried to refresh in the ajax and nothing happened? How do i do this right? How do i do this right?

Controller

if(request()->ajax()) {

            //do something             
            return ['success' => 'successfully done'];
            return redirect('admin/all')->with('status','Successfully done!');

JS

<script type="text/javascript">

        $('#master').on('click', function(e) {
         if($(this).is(':checked',true))  
         {
            $(".sub_chk").prop('checked', true);  
         } else {  
            $(".sub_chk").prop('checked',false);  
         }  
        });

        $('.approve_all').on('click', function(e) {

            var allVals = [];  
            $(".sub_chk:checked").each(function() {  
                allVals.push($(this).attr('data-id'));
            });  

            if(allVals.length <=0)  
            {  
                alert("Please select row.");  
            } 

            else {  



                var check = confirm("Are you sure you want to delete this row?");  
                if(check == true){  

                    var join_selected_values = allVals.join(","); 

                    $.ajax({
                        url: $(this).data('url'),
                        type: 'GET',
                        headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                        data: 'ids='+join_selected_values,

                        success: function (data) {
                            if (data['success']) 
                            {

                                $("#" + data['tr']).slideUp("slow");
                                alert(data['success']);
                                location="/admin/all";


                            } 
                            else if (data['error']) 
                            {
                                alert(data['error']);
                            } 
                            else 
                            {
                                //alert('Whoops Something went wrong!!');
                            }
                        },
                        error: function (data) {
                            alert(data.responseText);
                        }
                    });
                                window.location.href="/your/url" ;

                  $.each(allVals, function( index, value ) 
                  {
                      $('table tr').filter("[data-row-id='" + value + "']").remove();
                  });
                }  

            }  


        $('[data-toggle=confirmation]').confirmation({
            rootSelector: '[data-toggle=confirmation]',
            onConfirm: function (event, element) {
                element.trigger('confirm');
            }
        });

        $(document).on('confirm', function (e) {
            var ele = e.target;
            e.preventDefault();

            $.ajax({
                url: ele.href,
                type: 'GET',
                headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                success: function (data) {
                    if (data['success']) 
                    {

                        $("#" + data['tr']).slideUp("slow");
                        alert(data['success']);
                        location="/admin/all";

                    } 
                    else if (data['error']) {
                        alert(data['error']);
                    } 
                    else 
                    {
                        alert('Whoops Something went wrong!!');
                    }
                },
                error: function (data) {
                    alert(data.responseText);
                }
            });

            return false;
        });



    });

</script>
like image 676
pogba Avatar asked Nov 25 '17 06:11

pogba


2 Answers

You need to use javascript to refresh the page. You can use location.reload()

 if ( data['success'] ) 
 {
     alert(data['success']);
     location.reload();
 } 
like image 84
Eddie Avatar answered Sep 28 '22 06:09

Eddie


For those of you still looking for a Solution, This is how you can resolve this issue.

First of all, just return a simple message in your controller.

class SettingsController extends Controller
{

public function __construct()
    {

    }

public function destroy($id)
    {

        $user = User::findOrFail($id);
        $user->delete();
        return 'Record successfully deleted';
    }
}

Secondly, Add this code in your javascript file to refresh the page.

setTimeout(function () { document.location.reload(true); }, 5000);

<script type="text/javascript">

        function deluser(id)
         {
            event.preventDefault();

            $.ajax({
             headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
             url:"{{ route('settings.destroy','')}}/"+parseInt(id),
             method: 'delete',
             data:{
                 id: id,
                 },
success: function(data)
            {
                $('#validation-message').append('<div class="alert dark alert-success alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span> </button><b>'+data+'</b></div>');
                setTimeout(function () { document.location.reload(true); }, 5000);

             },
            error: function(xhr)
            {
                $('#validation-message').html('');
                $.each(xhr.responseJSON.errors, function(key,value) {
                    $('#validation-message').append('<div class="alert dark alert-danger alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span> </button><b>'+value+'</b></div>');
                    });
            }
            });
         }

      </script>

Finally in your HTML.

<!DOCTYPE html>
<html>
<body>
<div class="col-xl-12 form-group" id="validation-message"></div>
<button onclick="deluser('ID_NUMBER_GOES_HERE')">Click me</button>
</body>
</html>
like image 30
eliarms Avatar answered Sep 28 '22 08:09

eliarms