local-host 2017-09-14 04:20 采纳率: 100%
浏览 18

无法更新数据

Route

Route::resource('/mediafile', 'MediaController');

MediaController

public function update(Request $request, $id)
{
    $media = Media::findOrFail($id);
    if($request->hasFile('UserFile')) {
        $image = $request->file('UserFile');
        $filename = $image->getClientOriginalName();  
        Image::make($image)->resize(300, 300)->save(public_path('media/' . $filename));
        $media->MediaPath = $filename;
        $media->MediaName = $filename;
        $media->Description = $request->Description ? $request->Description : '';
        $media->save();
    }

    return response()->json($media);

}

View

<form class="form-horizontal" id="media-form" enctype="multipart/form-data">
    {{ csrf_field() }}
        <div class="form-group">
            <div class="col-xs-6 col-sm-6 col-md-6">
                <label for="MediaName">Nama Media</label>
              @if($edit)
              <input type="text" id="medianame" class="form-control" name="MediaName" value="{{$mediaEdit != null ? $mediaEdit->MediaName : ''}}">
              @else
                <input type="text" id="medianame" class="form-control" name="MediaName">
              @endif
                @if ($errors->has('MediaName'))
                <span class="help-block">
                    <strong>{{ $errors->first('MediaName') }}</strong>
                </span>
                @endif
            </div>
        </div>

        <div class="form-group">
            <div class="col-xs-6 col-sm-6 col-md-6">
                <label for="Description">Description</label>
            @if($edit)
            <textarea class="form-control" name="Description" style="height: 200px">{{$mediaEdit->Description}}</textarea>
                @else
            <textarea class="form-control" name="Description" style="height: 200px"></textarea>
            @endif
                @if ($errors->has('Description'))
                <span class="help-block">
                    <strong>{{ $errors->first('Description') }}</strong>
                </span>
                @endif
            </div>
        </div>

        <div class="form-group">
            <div class="col-xs-6 col-sm-6 col-md-6">
                <label for="Gambar">Gambar</label>
            @if($edit)
            <img src="{{ asset('media/' . $mediaEdit->MediaPath) }}" style="height: 150px;margin-left: 10px">
            <textarea readonly="" class="select valid" style="height:30px; width: 100%; margin-top: 10px">{{ asset('media/' . $mediaEdit->MediaPath) }}</textarea>
                @endif
            <input type="file" name="UserFile">
            </div>
        </div>

        <input type="hidden" name="_token" value="{{ csrf_token() }}"></input>
        <input type="hidden" name="_method" value="post"></input>
        <button type="submit" class="btn btn-info">Submit</button>

    </form>

Ajax

$('#media-form').submit(function(){
var formData = new FormData(this);
swal({
  title: 'Are you sure?', 
  type: 'info',
  showCancelButton: true,
  confirmButtonColor: "#DD6B55",
  confirmButtonText: "Confirm!",
  closeOnConfirm: false,
  closeOnCancel: false
  },
    //function
    function(isConfirm){
      if(isConfirm){
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
        $.ajax({
          type: "<?php echo $actionMethod; ?>",
          url: "<?php echo $actionURL; ?>",
          data: formData,
          dataType: 'json',
          processData: false, // Don't process the files
            contentType: false, // Set content type to false as jQuery will tell the server its a query string request
        })
        .done(function(data){
          if(data.id){
              swal({
                title: "Saved!",
                text: "Your Category has been saved.",
                type: "success"}, function(){
                  window.location.href = "<?php echo url('mediafile'); ?>";
              });
            }else{
              swal("Try again");
            }
            console.log(data);
        })
        .error(function(data){
            swal("Cancelled", "Please fill the data first.");
            console.log('Error:', data);
        });
      } else{
        swal("Cancelled");
      }
    //end function
});
return false;
});

I put the MediaName, Description, and UserFile

enter image description here

when I clicked submit, I got data like this

enter image description here

I can not update the data, when I try to update them, my data have not changed. I don't know why, I think that I did not do something wrong in my code.. Please help me to solve this if you know the answer

  • 写回答

2条回答 默认 最新

  • 胖鸭 2017-09-14 05:17
    关注
    <form  name="itemGroupForm" action="{{URL::to('editItem',$items[0]->id)}}"   id="itemGroup" data-parsley-validate="" method="post"  onsubmit="return submitFormItemGroup();">
    
         function submitFormItemGroup() {
      var form_data = new FormData(document.getElementById("itemGroup"));
      form_data.append("label", "WEBUPLOAD");
      $.ajax({
          url: "{{URL::to('editItem',$items[0]->id)}}}",
          type: "POST",
          data: form_data,
          processData: false,  // tell jQuery not to process the data
          contentType: false   // tell jQuery not to set contentType
      }).
    
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)