2016-12-16 48 views
1

我有一個問題與在控制器代碼點火器中傳遞兩個Ajax形式有關。我的第一種形式是一個文件var formData = new FormData($('#form-upload')[0]);ajax通過codeigniter傳遞兩種形式

和我的第二種形式是由配置文件數據的$('#frm_patientreg').serialize()

現在我的問題是我怎麼能傳遞阿賈克斯這兩種形式? 我已經嘗試過這樣的代碼:

var fileToUpload = inputFile[0].files[0]; 
    if(fileToUpload != 'undefine') { 
      var formData = new FormData($('#form-upload')[0]); 
      $.ajax({ 
       type: "POST", 
       url: siteurl+"sec_myclinic/addpatient", 
       data: $('#frm_patientreg').serialize()+formData, 
       processData: false, 
       contentType: false, 
       success: function(msg) { 
        alert("Successfully Added"); 
        $('#frm_patientreg')[0].reset(); 
       } 
      }); 
    } 
    else { 
     alert("No File Selected"); 
    } 

但它返回我一個錯誤。 當我試圖只通過data:formData,,我的圖像文件已成功上傳,但是當我添加$('#frm_patientreg').serialize()時,它會輸出一個錯誤。我怎樣才能通過這兩種形式?

這裏是我的控制器:

public function addpatient() { 
     $config['upload_path'] = './asset/uploaded_images/'; 
     $config['allowed_types'] = 'gif|jpg|jpeg|png'; 
     $config['max_size'] = 1024 * 8; 
     $this->load->library('upload', $config); 
     if($this->upload->do_upload("file")) { 
      $upload_data = $this->upload->data(); 
      $file_name = base_url().'asset/uploaded_images/'.$upload_data['file_name']; 

      $mypatiendid = $this->genpatient_id(); 
      $patient_bday = $this->input->post('pabdate'); 
      $DB_date = date('Y-m-d', strtotime($patient_bday)); 
      $patient_height = $this->input->post('paheight'); 
      $DB_height = $patient_height . " cm"; 
      $patient_weight = $this->input->post('paweight'); 
      $DB_weight = $patient_weight . " kg"; 

      $data = array (
        'patient_id' => $mypatiendid, 
        'patient_fname' => $this->input->post('pafname'), 
        'patient_mname' => $this->input->post('pamname'), 
        'patient_lname' => $this->input->post('palname'), 
        'patient_address' => $this->input->post('paaddress'), 
        'patient_contact_info' => $this->input->post('pacontact'), 
        'patient_bday' => $DB_date, 
        'patient_age' => $this->input->post('paage'), 
        'patient_height' => $DB_height, 
        'patient_weight' => $DB_weight, 
        'patient_sex' => $this->input->post('psex'), 
        'patient_civil_status' => $this->input->post('pmartialstat'), 
        'patient_photo' => $file_name, 
      ); 
      var_dump($data); 
     } 
     else { 
      echo "File cannot be uploaded"; 
      $error = array('error' => $this->upload->display_errors()); var_dump($error); 
     } 
    } 
+0

爲什麼不使用單個表單並單獨發送所有值。你使用html和css單獨顯示它們。 –

+0

如果您使用單獨的表單,那麼我認爲對於第二種形式,您必須爲第二種形式的每個元素添加「$ .each」形式的數據.. –

+0

我該怎麼做@RahulSharma –

回答

0

不tested..but試試這個:

var FormTwo = new FormData(); 
$('#frm_patientreg input, #frm_patientreg select').each(function(index){ 
    FormTwo.append($(this).attr('name'),$(this).val()); 
}); 

FormTwo.append('file', $('#frm_patientreg input[type=file]')[0].files[0]); 

$.ajax({ 
      type: "POST", 
      url: siteurl+"sec_myclinic/addpatient", 
      data: {formTwo: FormTwo, formOne: formData}, 
      processData: false, 
      contentType: false, 
      success: function(msg) { 
       alert("Successfully Added"); 
       $('#frm_patientreg')[0].reset(); 
      } 
     }); 
+0

好吧,我會嘗試這個先生 –

+0

先生,我的frm_patientreg在那裏? –

+0

檢查我的更新回答.. –

0

改變這種

data: $('#frm_patientreg').serialize()+formData, 

這個

data: $('#frm_patientreg').serialize()+'&'+formData, 
+0

您好先生,我已經試過這種方式 –

+0

你能告訴我你有什麼錯誤嗎?您在頁面控制檯中檢查錯誤 –