Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this script that will upload multiple files and it will be retrieve by the controller.

here is the controller

function insertAttachment() {
    $i = 0;
    $referenceNo = $this->input->post('reference');

    if(!isset($_FILES[$i]) ) {

    }

    else {
        $x = $_FILES[$i]['name'];
        $xx = explode('.', $x);

        $config['upload_path'] = 'MRS-files\Upload_files';
        $config['allowed_types'] = 'xls|doc|jpg|png|gif|pdf';
        $this->load->library('upload',$config);

        for($i; $i <= 4; $i++) {
            $counter = $_FILES;

            while ( $i <= count($counter) ) {
                $x = $_FILES[$i]['name'];
                $xx=explode(".", $x);

                $config['file_name']= 'IT2015' .'('. ($i+1) .').'. $xx[1];
                $this->upload->initialize($config);

                $_FILES['up']['name']       = $_FILES[$i]['name'];
                $_FILES['up']['tmp_name']   = $_FILES[$i]['tmp_name'];
                $_FILES['up']['type']       = $_FILES[$i]['type'];
                $_FILES['up']['size']       = $_FILES[$i]['size'];

                if ( ! $this->upload->do_upload('up')) {
                    //error on uploading
                    echo str_replace('','',$this->upload->display_errors()); //temporary commented no use cause of redirect to homepage
                    //$this->cancelREC();
                    exit();
                }

                else {
                    $data = array('upload_data' => $this->upload->data());
                    $this->edit_development_model->insertonAttachments($data['upload_data'] , $referenceNo);
                    $i++;
                }
            }
        }
    }
}

here is the script

function EditUploadImage() {
    var data = new FormData($('input[name^="edit_files"]'));

    jQuery.each($('input[name^="edit_files"]')[0].files, function(i, file) {
        data.append(i, file);
    });

    $.ajax ({
        type: 'POST',
        data: data,
        url: 'mis.php/edit_development_detailsControl/updateRequest',
        cache: false,
        contentType: false,
        processData: false,
        success: function(data) {
            alert(data);
            //messagealert("Success","You're Request have been save successfully");
        }
    });
}

my problem here is that how can i put another data in the data: section in the ajax request for example like this:

data: data + '&reference='+$('#ref').val(),

share|improve this question
1  
Cantt you just data.append("reference", $('#ref').val()); –  Naruto 19 hours ago
1  
seems you are looking for something like this... stackoverflow.com/questions/21060247/… –  Bharathi Raja 18 hours ago
    
BTW your FormData constructor is wrong, it should take a HTMLFormElement or nothing –  Musa 18 hours ago
    
Thank you guys. –  Dale 9 hours ago

1 Answer 1

up vote 0 down vote accepted

Hope this one help you.

var fd = new FormData();
    var file_data = $('input[type="file"]')[0].files; // for multiple files
    for(var i = 0;i<file_data.length;i++){
        fd.append("file_"+i, file_data[i]);
    }
    var other_data = $('form').serializeArray();
    $.each(other_data,function(key,input){
        fd.append(input.name,input.value);
    });
    $.ajax({
        url: 'test.php',
        data: fd,
        contentType: false,
        processData: false,
        type: 'POST',
        success: function(data){
            console.log(data);
        }
    });
share|improve this answer
    
can input.name be like $('#ref').val()? –  Dale 9 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.