【问题标题】:ajax passing two forms with codeigniterajax 使用 codeigniter 传递两种形式
【发布时间】:2017-05-02 08:22:04
【问题描述】:

我在将 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);
        }
    }

【问题讨论】:

  • 为什么不使用单一形式并以单一形式发送所有值。 u 使用 html 和 css 将它们分开显示。
  • 如果您使用单独的表单,那么我认为对于第二个表单,您必须为第二个表单的每个元素附加带有$.each 的表单数据..
  • 我该怎么做@RahulSharma
  • @Purushottamzende 谢谢你的回复先生,我的一个表格是文件选择和上传先生,。这就是我把它分开的原因

标签: javascript php ajax codeigniter image-uploading


【解决方案1】:

未测试..但试试这个:

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

【讨论】:

  • 先生,我的 frm_patientreg 在哪里?
  • 如何在我的控制器中显示它?
  • 先生,我将编辑我的问题,我将包括我的控制器代码
  • 你也要上传图片吗?
  • 您可以像获取帖子数据一样访问该数据,例如:$this->input->post('input-name')
【解决方案2】:

改一下

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

进入这个

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

【讨论】:

  • 你能告诉我你遇到了什么错误吗?您在页面控制台中检查错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 2015-06-23
相关资源
最近更新 更多