Jquery function does not access variables from the form

Asked

Viewed 45 times

0

I need to calculate the value of a service of a petshop project, where it is necessary to inform the data: id_porte (small, medium, large), id_categoria_animal (dog, cat), and what the service (bath, Tosa or bath with Tosa), because the values vary according to the size of the animal, category and service requested.

But the information id_categoria_animal and id_porte is already filled in the form when the customer’s CPF is entered in the previous page, so when the service is selected in a field select, automatically loads the value, but it’s not working and I haven’t found 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 that 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 jquery to run, which is inside the/ajax controller

<?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>";

Retorno da aba network no navegador

  • 1

    Put a console.log(data) on the return of Ajax to see what returns.

  • it only recovers the id_servico

1 answer

0

I was able to get the variables as follows:

var id_cat_animal = $('input#id_cat_animal').val();
var id_porte = $('input#id_porte').val();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.