简体   繁体   中英

ajax passing two forms with codeigniter

I have a problem related with passing two forms in ajax to my controller code igniter. My first form is a file var formData = new FormData($('#form-upload')[0]);

and my second form consists of profile data $('#frm_patientreg').serialize()

now my problem is how can I pass these two forms in ajax? I already tried this code:

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

but it returns me an error. When I tried to pass data:formData, only, my image file was successfully uploaded, but when I add the $('#frm_patientreg').serialize() , it outputs an error. How can I pass both forms?

Here is my controller:

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

Not tested..but try this:

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

change this

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

into this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM