发送多个内容类型的Ajax

时间:2017-06-07 09:50:03

标签: javascript php jquery ajax codeigniter

我使用带有codeigniter的ajax成功上传图片。但是现在我想发送数组,所以我可以在控制器和模型中插入数据。

这是我的代码,以便更好地解释:

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

这是脚本代码

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

formData正在正确发送。但asidvalue为空。所以我无法使用控制器和模型插入数据。 我该如何解决这个问题?

如果您想查看:this is my post

,这是完整的代码

当我尝试使用控制台或警报时,该值存在。

2 个答案:

答案 0 :(得分:0)

发送Ajax数据时,发送(name:string,value:string)对。由于cmbvalue是一个数组,你需要将其转换为字符串。可以是数组到CSV或一些可以解析的字符串。 如果cmbvalue = [apple,banana]你可能想把它作为字符串cmbval =“apple,banana”发送,同时在数据中发送它并解析服务器端的csv / string。 在Javascript中,使用toString()

将数组转换为字符串
data: 
{
    formData : formData,
    asid : asid_quiz,
    value:cmbval.toString();
}

答案 1 :(得分:0)

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