简体   繁体   中英

Send multiple content-type Ajax

I have succeed upload image using ajax with codeigniter. But now i want to sending array too so i can insert data inside controller and model.

This is my code for better explanation :

<form method="POST" id="quiz_file" action="<?php echo site_url('home/upload_quiz/' . $kelas);?>" enctype="multipart/form-data">
<input name="filequiz" id="filequiz" type="file" />
</form>

This is the controller :
function upload_quiz($kelas) 
{
    $temp = explode(".", $_FILES["filequiz"]["name"]);
    $extension = end($temp);
    $new_name = time() . "." . $extension; 
    $config['upload_path']          = './assets/images/quiz_images/';
    $config['allowed_types']        = 'gif|jpg|png|jpeg';
    $config['max_size']             = 2000;
    $config['file_name']            = $new_name;
    $config['max_width']            = 10000;
    $config['max_height']           = 10000;
    $this->load->library('upload', $config);

    $session_data = $this->session->userdata('logged_in');
    $data['user_name'] = $session_data['user_name'];
    $data['kelas'] = $kelas;
    if ( ! $this->upload->do_upload('filequiz'))
    {
        print_r(array('error' => $this->upload->display_errors()));
        $data['error'] = array('error' => $this->upload->display_errors());
        $this->load->view('course', $data);
    }
    else
    {
        $this->load->model('Model');
        $asid = $this->input->post('asid');
        $value = $this->input->post('value');
        $this->Model->inputassignmentscore($asid,$value); <---- The error come from this
        $data['status'] = array('upload_data' => $this->upload->data());
        $quiz_name = $this->input->post('quiz_name');
        if($this->Model->input_quiz($quiz_name,$new_name,$kelas) == TRUE)
        {   
            echo $asid;
            $this->load->view('course', $data);
        }
        elseif($this->Model->input_quiz($quiz_name,$new_name,$kelas) == FALSE)
        {
            $this->load->view('course', $data);
        }
    } 
} 

This is the script code

var cmbvalue = [];
if(asid_quiz.length > 0)
{
    for(i=0;i<asid_quiz.length;i++)
    {
        cmbass = document.getElementsByName('quizcmb')[i].value;
        cmbvalue.push(cmbass);
        console.log('cmbass : ' + cmbvalue);
    }
}

$.ajax({
url: uploadURI,
type: 'post',
data: 
{
    formData : formData,
    asid : asid_quiz,
    value : cmbvalue
},
processData: false,
contentType: false,
beforeSend: function ( xhr ) {
},
success: function(data) 
{
}
});

The formData is sending correctly. But the asid and value is empty. So i cannot insert data using controller and model. How can i fix this ?

This is the complete code if you want to see : this is my post

When i try using console or alert, the value is exists.

while sending Ajax data, you send (name: string,value:string) pair.Since cmbvalue is an array, you need to convert it into string. May be array to CSV or some string that can be parsed. if cmbvalue=[apple,banana] you might want to send it as string cmbval="apple,banana" while sending it in data and parse csv/string in server side. In Javascript, to convert array to string using toString()

data: 
{
    formData : formData,
    asid : asid_quiz,
    value:cmbval.toString();
}
var asid_quiz = ['1','2','3'];
var cmbvalue = [];
for(i=0;i<asid_quiz.length;i++)
    {
        cmbass = asid_quiz[i];
        cmbvalue.push(cmbass);
        console.log('cmbass : ' + cmbvalue);
    }
$.ajax({  
    type: 'POST',  
    url: 'test.php', 
    data: { album: 'test',asid:'asid_quiz',value:cmbvalue },

    success: function(response) {
        console.log(response);
    }
});

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