1
I am uploading a file with jquery+ajax using the jquery form plugin.
The form processing is all ok, the file goes up and the data is recorded in the database. The problem is in the return with the json response. It is printing the response string.
View:
$("#salvar-fcei").click(function () {
// bind form using ajaxForm
$('#jsonForm').ajaxForm({
// dataType identifies the expected content type of the server response
dataType: 'json',
// success identifies the function to invoke when the server response
// has been received
success: function (response) {
if (response.mensagem == 'ok') {
$(".modal-title").html("Cadastro realizado com sucesso!");
$(".modal-text").html("O gestor foi adicionado com sucesso ao Projeto.");
$("#modal").addClass('modal-success');
$("#modal").modal();
} else {
$(".modal-title").html("Ops! Verifique os erros abaixo");
$(".modal-text").html(response.mensagem);
$("#modal").addClass('modal-warning');
$("#modal").modal();
}
}
});
});
A tag form:
<form class="form-horizontal" id="frm-fcei" method="POST" enctype='multipart/form-data' action="<?php echo base_url('projeto/add_fcei'); ?>">
The Controller:
function add_fcei(){
$response = $this->model_projeto->add_fcei();
echo json_encode($response);
}
The Model
function add_fcei() {
$config['upload_path'] = './fcei';
$config['allowed_types'] = 'pdf';
$config['file_name'] = 'FCEI-'.$this->input->post('id_projeto').'-'.$this->input->post('numero');
$this->load->library('upload', $config);
if ($this->upload->do_upload('arquivo')){
$data['id_projeto'] = $this->input->post('id_projeto');
$data['numero'] = $this->input->post('numero');
$data['dt_emissao'] = $this->input->post('dt_emissao');
$data['arquivo'] = $this->upload->data('file_name');
$data['dt_cadastro'] = date('Y-m-d');
$data['status'] = '1';
if ($this->db->insert('cadastro_fcei',$data)){
$response = ['mensagem'=>'ok'];
} else{
$response = ['mensagem'=>'Não foi possível cadastrar no banco de dados. Arquivo foi salvo.'];
}
} else{
$response = ['mensagem'=>$this->upload->display_errors()];
}
return $response;
}
When I submit the form it prints on the array screen
{'mensagem':'ok'}
Hello Fabrício, could you clarify what you want to do? The output seems to be according to your code, this is
$response = ['mensagem'=>'ok'];
. What exactly do you want to happen?– Anthony Accioly