从使用 jQuery 和 CodeIgniter 上传文件(但没有页面刷新)
@cbrandolino 建议我使用 jQuery 表单插件来解决我的问题。除了一个问题外,它运行良好,回调函数没有像我预期的那样触发。
这是我修改后的代码
++ Javascript ++
$('#send_button').live('click', function(){
var options = {
url: 'formHandle/add',
success: function(){
alert('something');
}
//I tried both function() or function(data)
//or a function created outside $(document).ready()
//None of them works
};
$('#new_entry_form').ajaxSubmit(options);
})
++ 控制器 ++
function add(){
$array = array(
'title' => $this->input->post('title'),
'description' => $this->input->post('description'),
'content' => $this->input->post('content'),
'start' => $this->input->post('start'),
'due' => $this->input->post('due'),
'price' => $this->input->post('price'),
'promotion_price' => $this->input->post('promotion_price'),
'website' => $this->input->post('website'),
'author' => $this->input->post('author'),
);
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['overwrite'] = false;
$config['remove_spaces'] = true;
$config['encrypt_name'] = true;
$this->load->library('upload', $config);
if($this->upload->do_upload('picture')){
$array['picture'] = $this->upload->file_name;
$this->load->model('entry_model');
$this->entry_model->insertEntry($array);
echo 'true';
//I tried $this->load->view, echo or return
//none of them works
}else{
echo 'false';
}
}
文件上传正常,文件已上传到特定文件夹。
数据插入工作正常,数据按预期插入数据库。
我唯一的问题是回调函数。
你知道如何解决这个问题吗?我需要显示数据插入的结果。
更新:
添加 return false 对我没有任何帮助。还是行不通