如何在ajax codeiginter中传递带有文件上传的帖子的额外字段

时间:2016-06-21 11:07:24

标签: php jquery ajax codeigniter

我使用ajax在php codeigniter项目中上传文件。该文件正在成功上传。但是我也希望用数据库向数据库发布一些额外的值。我不知道该怎么做。任何人都可以告诉我如何在php中保存文件时传递另一个数据字段

这是我的js代码

$("#btnupdatecover").click(function(event){

    alert(coverPostion);

     if($("#fileuploadcover").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() {
            var myXhr = $.ajaxSettings.xhr();
            return myXhr;
      },
      cache:false,                    
      contentType: false,
      processData: false,
      success: function(result){


       toastr8.info({
      message:'Profile Picture Updated', 
        title:"New Image Uploaded",
        iconClass: "fa fa-info",
           // imgURI: ["https://unsplash.it/120/120?image=20"]
    });
       clearAll();
      }                       
    });
    // 
     event.preventDefault();

        }  

}
else
{
     toastr8.info({
      message:'Error Occured', 
        title:"Please try again",
        iconClass: "fa fa-info",
   // imgURI: ["https://unsplash.it/120/120?image=20"]
    });
}

});

我的PHP代码

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;
    }
    }

我只是传递额外的字段来发布数据库。

谢谢

1 个答案:

答案 0 :(得分:0)

你可以做到这一点:

方法1

在表单中使用隐藏字段。

方法2

$.ajax({
      type: "POST",
      url: "Userpage/updatedp",
      data: {formData: formData, var1: 'value', var2: 'value'},
      dataType: 'json',
      success: function(result){


       toastr8.info({
      message:'Profile Picture Updated', 
        title:"New Image Uploaded",
        iconClass: "fa fa-info",
           // imgURI: ["https://unsplash.it/120/120?image=20"]
    });
       clearAll();
      }                       
    });

现在这些价值你将获得你的控制器。