douluan5738 2016-11-17 02:57
浏览 60

Laravel 4使用ajax删除文件

Please help me solve my problem in my delete file in my project i am confuse because it always return me a null value

here is my editfile.blade.php

@include('partials.navbar')
<link rel="stylesheet" type="text/css" href="http://localhost:8000/assets/css/jquery.dataTables.min.css">
<link rel="stylesheet" type="text/css" href="http://localhost:8000/assets/css/search.css">
<!-- Search -->
<div class="container">
  <div class="row">
    <div class="col-md-12">
            <div class="input-group" id="adv-search">
                <input type="text" class="form-control" placeholder="Search file" />
                <div class="input-group-btn">
                    <div class="btn-group" role="group">
                        <div class="dropdown dropdown-lg">
                            <button type="button" class="set-width btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false"><span class="caret"></span></button>
                            <div class="dropdown-menu dropdown-menu-right" role="menu">
                                <form class="form-horizontal" role="form">
                                  <div class="form-group">
                                    <label for="file">File type</label>
                                    <select class="form-control">
                                        <option value="pf">Public Weather Forecast</option>
                                        <option value="sf">24 Shipping Forecast</option>
                                        <option value="gale">Gale Warning Forecast</option>
                                        <option value="advisory">Weather Advisory</option>
                                        <option value="tca">Tropical Cyclone Advisory</option>
                                        <option value="swb">Severe Weather Bulletin</option>
                                        <option value="iws">International Warning for shipping</option>
                                        <option value="wof">Weather Outlook Forecast</option>
                                        <option value="spf">Special Forecast</option>
                                        <option value="sm">Surface Maps</option>
                                    </select>
                                  </div>
                                  <div class="form-group">
                                    <label for="contain">Date</label>
                                    <input class="form-control" type="date" />
                                  </div>
                                  <div class="form-group">
                                    <label for="contain">Contains the words</label>
                                    <input class="form-control" type="text" />
                                  </div>
                                  <button type="submit" class="btn btn-primary"><i class="fa fa-search"></i></button>
                                </form>
                            </div>
                        </div>
                        <button type="button" class="btn btn-primary"><i class="fa fa-search"></i></button>
                    </div>
                </div>
            </div>
          </div>
        </div>
  </div>
<!-- datatable-->
<div class="container">
    <div class="row">
      <div class="col-md-12">
        <h4>File Management</h4>

      <table id="mytable2" class="display" cellspacing="0" width="100%">

    <thead>
    <tr>
    <thead>
            <tr>
                <th>File Name</th>
                <th>Date Issued</th>
                <th>Uploader</th>

                <th>Actions</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th>File Name</th>
                <th>Date Issued</th>
                <th>Uploader</th>

                <th>Actions</th>
            </tr>
        </tfoot>
        <tbody>

    <tbody>
    @foreach ($files as $files3)
    <tr>
    <td>{{ $files3->file_name }}</td>
    <td>{{ $files3->date }}</td>
    <td>{{ $files3->username }}</td>
    <td><a href={{URL::to('/fileDetails?id=') . $files3->id}} class="btn btn-primary btn-xs edit-button" data-title="Edit" ><i class="fa fa-pencil"></i></a>
    <button data-id="{{ $files3->id }}" class="btn btn-danger btn-xs delete-button" data-title="Delete" data-toggle="modal" data-target="#delete-modal" ><i class="fa fa-trash"></i></button></td>
    </tr>
    @endforeach 

    </tbody>

</table>

<div class="clearfix"></div>

            </div>

        </div>
  </div>
</div>



    <div class="modal fade" id="delete-modal" tabindex="-1" role="dialog" aria-labelledby="delete-modal" aria-hidden="true">
      <div class="modal-dialog">
    <div class="modal-content">
          <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="fa fa-times"></i></button>
        <h4 class="modal-title custom_align" id="Heading">Delete this file record</h4>
      </div>
          <div class="modal-body">
       <form class="form-horizontal" action="/deletefile" method="post">
       <input  name="deletefileid" type="hidden" id="deletefileid" value="">
       <div class="alert alert-danger"><i class="fa fa-exclamation-triangle"></i> Are you sure you want to delete this Record?</div>

      </div>
        <div class="modal-footer ">
        <button type="submit" class="btn btn-success" ><i class="fa fa-check"></i> Yes</button>
        <button type="submit" class="btn btn-default" data-dismiss="modal"><i class="fa fa-times"></i> No</button>
      </form>
      </div>
        </div>
    <!-- /.modal-content --> 
  </div>
      <!-- /.modal-dialog --> 
    </div>
  </div>
  </div>
</div>
@include('partials.footer')
<script type="text/javascript" src="http://localhost:8000/assets/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="http://localhost:8000/assets/js/datatable2.js"></script>
<script src="http://localhost:8000/assets/js/modernizr-custom.js"></script>
<!-- polyfiller file to detect and load polyfills -->
<script src="http://localhost:8000/assets/js/polyfiller.js"></script>
<script>
  webshims.setOptions('waitReady', false);
  webshims.setOptions('forms-ext', {types: 'date'});
  webshims.polyfill('forms forms-ext');
</script>
 <script>
 $(function() {
 $(".delete-button").click(function(){
     var param = $(this).data('id');
     $.ajax({
         url: "/erasefile/" + param,
         success: function(msg){
           var info = JSON.parse(msg)[0];
           console.log(info);
           $('#deletefileid').val(info.id);
         },
         error:function(){
             alert("failure");
         }
     });
     });
});
</script>

here is my model file.php

public static function getdeleteFiles($deleteid)
    {
        $files = Files::find($deleteid);
        $files->delete();
    }   

here is my Controller FileController.php

public function eraseFile($id)
    {
        $files = Files::where('id',$id)
                     ->get();
        return json_encode($files);
    }

    public function deleteFile()
    {
        dd(Input::all());
        $ftype = Input::get('type');
        $oldfile = Input::get('oldfile');
        $deleteid = Input::get('deleteid');
        $delete = Files::getdeleteFiles($deleteid);
        //dd($deleteid);
        if ($delete == true) {
            File::delete(public_path("uploads/{$ftype}/{$oldfile}"));
            return Redirect::to('/edit');
        } else {
            return Redirect::back()->with('message', 'Delete, Failed');
        }   
    }

and here is my route

Route::get('/erasefile/{id}', 'FileController@eraseFile');
Route::post('/deletefile', array('uses' => 'FileController@deleteFile'));

Please help me solve this TIA!

  • 写回答

1条回答 默认 最新

  • duanchun1852 2016-11-17 03:27
    关注
    1. the input and select elements in your form does not have a name attribute

    2. your function didn't return any value

      public static function getdeleteFiles($deleteid)
      {
          $files = Files::find($deleteid);
          $files->delete();
      }
      

      maybe should be

      public static function getdeleteFiles($deleteid)
      {
          $files = Files::find($deleteid);
          return $files->delete();
      }
      
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度