I need to calculate the value of a service of a pethop project, where it is necessary to inform the data: id_porte
(small, medium, large), id_categoria_animal
(dog, cat) tosa or bath with tosa), since the values vary according to the size of the animal, category and the requested service.
However, the id_categoria_animal
and id_porte
information is already filled in the form when the customer's CPF is entered on the previous page, so when the service is selected in a select
field, the value automatically loads but is not giving right and did not find the error, I'm using the codeigniter framework.
This is the controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
Class Consulta_cpf extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->model('cliente/dados_cliente', 'dados');
$this->load->helper('date');
}
public function index(){
$this->form_validation->set_rules('cpf', 'CPF', 'required|trim|exact_length[11]|numeric');
//VERIFICA SE AS REGRAS COM O CPF DIGITADO FORAM EXECUTADAS.
if($this->form_validation->run() == FALSE){
if(validation_errors()){
die('<p> <a href="'.base_url('agendar_servico').'"> Clique aqui </a> para voltar </p>'.validation_errors());
}
}
if(isset($_POST['acao']) && $_POST['acao'] == 'Pesquisar >>'){
$cpf = $this->input->post('cpf');
//VERIFICA SE O CPF DO CLIENTE EXISTE NO BANCO DE DADOS.
try {
$pessoa = $this->dados->select_nome_pessoa($cpf);
} catch (Exception $e) {
die('<p> <a href="'.base_url('agendar_servico').'"> Clique aqui </a> para voltar</p>'.$e->getMessage());
}
$id_pessoa = $pessoa['id_pessoa'];
$id_cliente = $this->dados->select_id_cliente($id_pessoa);
$animal = $this->dados->select_animal($id_cliente);
$dados['nome_cliente'] = $pessoa['nome_pessoa'];
$dados['id_cliente'] = $this->dados->select_id_cliente($id_pessoa);
$dados['id_animal'] = $animal['id_animal'];
$dados['nome_animal'] = $animal['nome_animal'];
$dados['id_cat_animal'] = $animal['id_cat_animal'];
$dados['nome_cat_animal'] = $animal['nome_cat_animal'];
$dados['id_porte'] = $animal['id_porte'];
$dados['nome_porte'] = $animal['nome_porte'];
$dados['idade'] = $this->dados->select_idade($animal['data_nasc_animal']);
$dados['data'] = $this->dados->select_data();
$dados['servicos'] = $this->dados->select_servicos();
$dados['consulta_func'] = $this->dados->consulta_func(1);
$dados['cpf'] = $this->input->post('cpf');
$this->load->view('cliente/agendar_servico', $dados);
}
}
}
This is the model (only the function that matters in this case is the value query)
public function consulta_valor($id_servico, $id_cat_animal, $id_porte){
$options = "<option> Valores </option>";
$this->db->select('*');
$this->db->from('valores');
$this->db->where(array('id_servico' => $id_servico, 'id_cat_animal' => $id_cat_animal, 'id_porte' => $id_porte));
$query = $this->db->get();
if($query->num_rows() > 0){
foreach($query->result() as $linha){
$options .= "<option value='{$linha->id_valor}'> $linha->valor </option>";
}
return $options;
}else{
return false;
}
}
This is the function for the jquery run, which is inside the controller / ajax
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
Class Valor extends CI_controller{
function __construct(){
parent::__construct();
$this->load->model('cliente/dados_cliente', 'dados_cliente');
}
public function get_valor(){
$id_servico = $this->input->post('id_servico');
$id_cat_animal = $this->input->post('id_cat_animal');
$id_porte = $this->input->post('id_porte');
sleep(1);
echo $this->dados_cliente->ff($id_servico, $id_cat_animal, $id_porte);
}
}
This is the header function with jquery.
$(function(){
$('#servico').change(function(){
$('#valor').attr('disabled', 'disabled');
$('#valor').html("<option> Carregando... </option>");
var id_servico = $('#servico').val();
$.post(base_url+'index.php/ajax/valor/get_valor',{
id_servico : id_servico
}, function(data){
$('#valor').html(data);
$('#valor').removeAttr('disabled');
});
});
});
And finally this is the view (well, just a part of it):
echo form_label('Serviços: ');
echo "<select id='servico' name='servico'>";
echo $servicos;
echo "</select>";
echo form_label('Valor: ', 'valor');
echo "<select id='valor' name='valor' disabled=''>";
echo "<option>valor</option>";
echo "</select>";