dongzhi5386 2017-03-21 07:17
浏览 38
已采纳

用ajax重新执行php循环

I am using laravel 5.4.

I have many clinics in my database. I just want to retrieve all the clinics and put a delete button on each clinic. Whenever a user clicks the delete button the the clinic should be removed from the database and clinics should be updated in the front end.

This is my code.

@foreach($doctor->clinics as $clinic)
    <form method="POST" 
        action="{{url('doctors/'.$doctor->id.'/removeclinic/'.$clinic->id)}}"
        id="formremoveclinic{{$clinic->id}}">
        {{csrf_field()}}
        <button class="btn btn-sm btn-danger pull-right">
            <span class="glyphicon glyphicon-remove"></span>
        </button>
    </form>
    <p class="text-muted">Clinic {{$i++}}</p>
    <p class="text-muted">{{$clinic->address}}</p>
    <hr>
    <script>
        $("#formremoveclinic{{$clinic->id}}").submit(function(e){
            $('#loading').show();
            e.preventDefault();
            $.ajax({
                url: "{{url('doctors/'.$doctor->id.'/removeclinic/'.$clinic->id)}}",
                type: "DELETE",
                data: new FormData(this),
                contentType: false,
                cache: false,
                processData:false,
                success: function(data){
                    $('#loading').hide();
                },
                error: function(data){
                    var errors = data.responseJSON;
                    console.log(errors);
                    $('#loading').hide();

                }           
            });
        });
    </script>
@endforeach

I don't want to reload the page whenever a clinic is removed. So, how can I re-execute this loop, whenever a clinic is successfully removed using ajax.

  • 写回答

1条回答 默认 最新

  • duanhun3273 2017-03-21 07:27
    关注

    A better way to approach this might be to just remove the row using javascript. In the same function where you hide the loader, you can also remove the form from the dom.

    Like so:

    @foreach($doctor->clinics as $clinic)
        <div id="clinic{{$clinic->id}}">
            <form method="POST" 
                action="{{url('doctors/'.$doctor->id.'/removeclinic/'.$clinic->id)}}"
                id="formremoveclinic{{$clinic->id}}">
                {{csrf_field()}}
                <button class="btn btn-sm btn-danger pull-right">
                    <span class="glyphicon glyphicon-remove"></span>
                </button>
            </form>
            <p class="text-muted">Clinic {{$i++}}</p>
            <p class="text-muted">{{$clinic->address}}</p>
            <hr>
            <script>
                $("#formremoveclinic{{$clinic->id}}").submit(function(e){
                    $('#loading').show();
                    e.preventDefault();
                    $.ajax({
                        url: "{{url('doctors/'.$doctor->id.'/removeclinic/'.$clinic->id)}}",
                        type: "DELETE",
                        data: new FormData(this),
                        contentType: false,
                        cache: false,
                        processData:false,
                        success: function(data){
                            $('#loading').hide();
    
                            // Fade out and remove the form element
                            $("#clinic{{$clinic->id}}").fadeOut(300, function() {
                                $(this).remove();
                            });
                        },
                        error: function(data){
                            var errors = data.responseJSON;
                            console.log(errors);
                            $('#loading').hide();
    
                        }           
                    });
                });
            </script>
        </div>
    @endforeach
    

    This way you don't have to write another ajax function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集