0

Hi there I am having a problem when I click button delete it deletes the folder with image inside and that is what I want but the page it is not refreshed so I do not know that is deleted till I click refresh this is the code:

            @if($question->question_image) 
            <img src="{{url('/')}}/images/questions/{{ $question->id }}/medium/{{ $question->question_image }}"  class="answer-image-create" id="question_image_box">
 <input onchange="readquestionURL(this);" type="file" id="question_file" name="question_image" style="display:none"/>
            @else
            <img src="{{ asset('noavatars/no-image-selected.png') }}" class="answer-image-create" id="question_image_box">
             <input onchange="readquestionURL(this);" type="file" id="question_file" name="question_image" style="display:none"/>
            @endif 
          </div>

    {{--       <button type="button" class="btn" id="question_upfile"
                  style="cursor:pointer; padding-left: 24px; padding-right: 15px;">
            <i class="fa fa-upload" aria-hidden="true">
            </i>@lang('buttons.upload_image')
          </button> --}}

            <button type="button" value="{{-- {{ $i }} --}}" class="btn btn-flat btn-default btn-sm delete_image" id="delete_image" title="@lang('buttons.remove_option')"><i class="fa fa-trash-o" aria-hidden="true"></i> </button>

and the javascript code is this:

<script>

        $(document).on("click", "button[id=delete_image]", function(data){
       var questionid = {{$question->id}};
       var element = 'questions';

       $.ajax({
        type: "POST",
        url: "{{ action('website\images\ImagesController@deleteImage') }}",
        data: {
            _token: "{{ csrf_token() }}",
          'id':questionid,
                'element' : element,

      },
       success: function(response) {
        console.log(response);
    },
    error: function(response){
        alert("fail");
    }

      });

         function readquestionURL(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                $('#question_image_box')
                    .attr('src', e.target.result)
            };

            reader.readAsDataURL(input.files[0]);
            }
        }
    });




    </script>

The controller is this:

 public function deleteImage($element =null, $id = null){
          if(request()->ajax()) {

            $id = request()->input('id');
            $element = request()->input('element');

        if($element != null && $id != null){
             Storage::disk('public')->deleteDirectory('/images/'.$element. '/'. $id);
          }
        return 1;
        }
    }

How can I achieve to delete the image and be refreshed at the same time. Can someone please help me thank you very much .

Will
  • 69
  • 14
  • Possible duplicate of [How do I reload the page after an AJAX call is done with a dialog?](https://stackoverflow.com/questions/15962223/how-do-i-reload-the-page-after-an-ajax-call-is-done-with-a-dialog) – Onix Oct 30 '17 at 14:59

1 Answers1

2

You can refresh the page using javascript

location.reload();

You'll need to add that line on your ajax's success method.

Lloople
  • 1,827
  • 1
  • 17
  • 31