Codeigniter - Dynamic form

Asked

Viewed 1,103 times

2

all right? Well.. I’m a beginner in the world of Web Programming and I’m trying to develop a codeigniter system. Can you help me? I’m trying to make a dynamic form in which I have a select that has the months of the year .. and I would like when changing this select it to query the Mysql database and change an input with the value for the month sealed. The problem is that I want to make this happen without giving Ubmit in the form because Submit already creates a kind of invoice. To make it easier to understand. I’m looking to create a form that generates an invoice according to the number of visits made during the month. Month I choose through select. It takes month, searches in BD and changes the value in input.

Thanks in advance.

1 answer

1

When I need to do an ajax on a controller I do so:

<script>
   $(function (){
       $.ajax({
           type: 'GET', // ou post
           url: '<?= site_url('controller/action'); ?>',
           dataType: 'json',
           success: trataRetorno //function que irá tratar o retorno
       });
   });

   function trataRetorno(retorno){
       // código que irá manipular o retorno da query
       // utilize console.log(retorno) para depurar o retorno
   }
</script>

On the controller:

public function action () {
    $this->load->model('Modelo_model');
    echo json_encode($this->Modelo_model->executa_query());
}

EDIT:

To pass the value from select to php you can do so:

javascript

// vamos criar um evento para capturar a mudança do valor no select... caso queira outro trigger é só alterar a linha abaixo.
$('select#IdDoSelect').on('change', function (){
    pegaValorDoMes($(this).val()); // se for utilizar um evento atribuido a outro objeto que não seja o seu select você também deve trocar esse $(this) pelo seletor apropriado do seu select
});

function pegaValorDoMes(valor) {
    $.ajax({
           type: 'GET', // ou post
           url: '<?= site_url('controller/action'); ?>',
           dataType: 'json',
           data: valor, // essa é a única novidade no javascript... serve para passar o valor do campo para o controller
           success: trataRetorno //function que irá tratar o retorno
       });
}

function trataRetorno(retorno){
       $('input#IdDoInput').val(retorno.valor);
}

config/Routes.php

$routes['controller/action/(:num)'] = 'controller/action/$1'; //essa configuração permite que a sua action receba o valor do get feito pelo ajax

controllers/controller.php

public function action($mes) {
    // caso você opte por usar POST é só tirar as configurações de GET e utilizar o $this->input->post(mes); que irá funcionar normalmente
    $this->load->model('Modelo_model');
    echo json_encode($this->Modelo_model->executa_query($mes)); // essa é a linha que possibilita que o javascript trabalhe com o resultado em forma de objeto
}

models/Modelo_model.php

public function executa_query($mes){
    // aqui você vai realizar sua query normalmente retornando o array de resultado
    $this->db->select('valor');
    $this->db->where(array('mes' => $mes));
    $this->db->get('tabela')->row_array();
}

Note: Remembering that this is a very crude example, just to give you the idea of what should be done. In case you still have any doubt just comment again that I try to deepen myself more.

  • Thank you, Raphael. I’m following that same line. But I don’t know how to pass the value from select to PHP, query, return the value to AJAX and fill in the input. This is where I get lost. Could you help me? Thank you.

  • @Harrisonvinicius It worked, man?

  • Opa. speaks Ai friend. Sorry but I’m a little lost in how to do the query I have 2 tables .. one for O.S. and another for Contracts.. and I need to count how many of the "extra visits" type had in the month and multiply by the extra visiting value that is in the contract table and add with the monthly fixed value that is also in the contract table.. When I do direct (mysql_query("SELECT * FROM os WHERE status = 'Extra Visit' AND MONTH(dataFinal) = $mes"), it works.. but only works with the $mes fixed variable. Does it have any way to do this?

  • how so only works with the variable fixed month? did not understand this part

  • @Harrisonvinicius some expensive novelty?

Browser other questions tagged

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