Good, I'm starting to use codeigniter with the upload library, and ajax to save an image, it is saved in a table image that has 3 attributes id_image (that is autoincrementable), route, and as foreign id_person. this part being clear my problem. is that when I submit, the next error in the console bounces:
POST link 500 (Internal Server Error).
I attach my html code:
<form id="formImagen" action="<?php echo base_url();?>imagenes/guardar" method="POST">
<input type="hidden" name="id_persona" value="<?php echo $getPersona->Id_persona; ?>">
<input type="file" name="imagen">
<button type="submit" class="btn btn-squared btn-info margin-inline">Subir Imagen</button>
</form>
in the input hidden I pick up the id of the person by a query.
the script:
$('document').ready(function(){
$("#formImagen").submit(function (event){
event.preventDefault();
var formData = new FormData($("#formImagen")[0]);
$.ajax({
url:$("form").attr("action"),
type:$("form").attr("method"),
data:formData,
cache:false,
contentType:false,
processData:false,
success:function(respuesta){
if (respuesta === 'exito'){
alert('guardado');
}
else{
console.log(respuesta);
alert('error al guardar');
}
}
});
});
});
and my controller:
public function guardar(){
if($this->input->is_ajax_request()){
$id = $this->input->post("id_persona");
$config = [
"upload_path" => "./assets/images/uploads",
'allowed_types' => "png|jpg|jpeg"
];
$this->load->library("upload",$config);
if($this->upload->do_upload('imagen')){
$data array("upload_data" => $this->upload->data());
$datos = array(
"Ruta" => $data['upload_data']['file_name'],
"Id_persona" => $id
);
if($this->Imagenes_model->guardar($datos)==true)
echo "exito";
else
echo "error";
}else{
echo $this->upload->display_errors();
}
}
else
show_404();
}