If a form is filled out and the next button is clicked but the user realized that there is a error and clicks on a previous button , how to do so When clicking on the previous button I returned the form that had been previously filled and that the fields of that form appear filled with the last record that had been entered, and thus avoid having to go back to fill out the form completely and only have to correct what is required.
CONTROLLER
public function carga_vista_anterior_1(){
$data['necesidades'] = $this->Servicios_model->get_ultima_necesidad($id_necesidad);
$resultado = $this->load->view('servicios/create_update_1',$data, TRUE);
$response = array('mensaje' => $resultado);
$this->output
->set_status_header(200)
->set_content_type('application/json', 'utf-8')
->set_output(json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES))
->_display();
exit;
}
MODEL
//trae el id mayor de la tabla de necesidad
public function get_ultima_necesidad($id_necesidad) {
$necesidades = $this->db->query('SELECT num_necesidad, descripcion_necesidad, tipos_necesidad_id_tipo_necesidad FROM necesidades WHERE id_necesidad in (select MAX(id_necesidad) from necesidades )');
return $necesidades->result_array();
}
JavaScript FUNCTION FOR THE 'PREVIOUS' BUTTON
$("#anterior_2").click(function(){
$.ajax({
method: "POST",
url: base_url+"/servicios/carga_vista_anterior_1",
data: $("#frm_create_servicio").serialize(), // Adjuntar los campos del formulario enviado.
dataType: 'json',
success: function(data) {
$('#principal').html(data.mensaje);
},
error: function(error) {
$('#contenido').html('<div class="alert alert-warning alert-dismissible" role="alert">'+
'<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>'+
'<stro ng>Error!!!</strong> Solicitud de AJAX no completada->'+error.status+'</div>');
}
});
return false;
});