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 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名