未定义的索引错误(Codeigniter)

时间:2016-07-05 06:51:23

标签: javascript php jquery ajax codeigniter

我的代码之前工作正常。但现在它没有用。我正在使用codeigniter,我正在使用jquery ajax上传文件。我不知道为什么我的代码停止工作。如果您能找到问题,请告诉我。

这是控制器代码

public function updatedp()
{   
    $var = $_FILES['fileUp'];
    $img=$_FILES['fileUp'];
    $config['upload_path'] = 'webim/dp_images'; 
    $config['overwrite'] = 'TRUE';
    $config["allowed_types"] = 'jpg|jpeg|png|gif';
    $config["max_size"] = '1400';
    $config["max_width"] = '1400';
    $config["max_height"] = '1400'; 
    $this->load->library('upload', $config);

    if(!$this->upload->do_upload('fileUp')) 
    {               
        $this->data['error'] = $this->upload->display_errors(); 
        echo json_encode(array("result"=>$this->data['error']));
        exit;
    } else { 
        $data=array('active'=>0);
        $this->db->where('userid','1');
        $this->db->update('music_user_dp',$data);

        $uname['uname'] =$this->session->all_userdata('uname');
        $uname['id'] =$this->session->all_userdata('id');
        $post_data = array(
            'id' => '',
            'userid' => $uname['id']['id'], 
            'profilepic'=>$var['name'], 
            'updatedate' => date("Y-m-d H:i:s"),
            'active' => '1'
        );
        $this->Userpage_model->insert_dp_to_db($post_data);
        echo json_encode(array("result"=>"Success"));
        exit;
    }
}

我的jquery代码调用上面的函数:

$("#btnupdate").click(function(event){
 if($("#fileupload2").val() != ''){
    if (typeof FormData !== 'undefined') {
      var form = $('#formname').get(0); 
      var formData = new FormData(form);  

    $.ajax({
      type: "POST",
      url: "Userpage/updatedp",
      data: formData,
      mimeType:"multipart/form-data",
      dataType: 'json',
      xhr: function() {
         return $.ajaxSettings.xhr();
      },
      cache:false,                    
      contentType: false,
      processData: false,
      success: function(result){


       toastr8.info({
         message:'Profile Picture Updated', 
         title:"New Image Uploaded",
         iconClass: "fa fa-info",
       });
      }                       
    });
     event.preventDefault();
        }  

  } else {
     toastr8.info({
       message:'Error Occured', 
       title:"Please try again",
       iconClass: "fa fa-info",
    });
  }  
});

HTML:

<div class="modal fade" id="myModal" role="dialog">
<form enctype="multipart/form-data" name="formname" id="formname"  method="post" action="">
  <div class="modal-dialog">     
    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header "> 
        <script type="text/javascript">  
        $(document).ready(function(){
          $('#mgupload-dp').click(function(e){ 
            $('#fileupload2').click(); 
            e.preventDefault();
          });
        });
        </script>
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Create profile picture</h4>
      </div>
      <div class="modal-body">
        <div class="text-center" style="width:100%"> <img src="<?php echo base_url(); ?>img/profile.png" alt="add dp" id="pop-dp" >
          <button type="button" class="btn btn-default text-center" id="mgupload-dp">Choose picture to upload</button>
          <input type="file" id="fileupload2" name="fileUp" class="hidden-dp" accept="image/*">
        </div>
        <div class="clearfix"></div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" id="btnupdate">Update Picture</button>
      </div>
    </div>
  </div>
  </div>
</form>
</div>

文件未上传,我收到此错误

  

遇到PHP错误   严重性:注意   消息:未定义的索引:fileUp   文件名:controllers / Userpage.php

1 个答案:

答案 0 :(得分:0)

  1. 在FF中打开Firebug。
  2. 单击控制台下的ajax调用URL。
  3. 查看&#34; Post&#34;标签。
  4. 如果存在fileUp,请使用$ this-&gt; input-&gt; post(&#39; fileUp&#39;);获取内容。