簡體   English   中英

Ajax通過Codeigniter傳遞兩種形式

[英]ajax passing two forms with codeigniter

我在將兩種形式的ajax傳遞給控制器​​代碼點火器時遇到問題。 我的第一個表單是文件var formData = new FormData($('#form-upload')[0]);

我的第二個表單由配置文件數據$('#frm_patientreg').serialize()

現在我的問題是如何在ajax中傳遞這兩種形式? 我已經嘗試過此代碼:

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

未經測試..但是請嘗試以下操作:

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

改變這個

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

進入這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM