Error loading dynamic modal combo with ajax and php

Asked

Viewed 37 times

0

Good afternoon! When filling out a modal form, I use two combos, which are dynamically loaded. However, when I edit the record, the result of the second combo does not return and is empty. Could you help me ? Below is the ajax I use to load the combos

//BUSCA GESTAO
$(document).ready(function(){
$.ajax({
    url : "<?php echo site_url('projeto/ajax_busca_gestao')?>",
    type: "GET",
    dataType: "JSON",
    success: function(data)
    {
      if (data.length > 0){    
          var option = '<option>Selecione..</option>';
          $.each(data, function(i, obj){
              option += '<option value="'+obj.id_grupos+'">'+obj.nivel_2+'</option>';
          })
          $('#grupo').html(option).show();
       }else{
           Reset();
           $('#mensagem').html('<span class="mensagem">Erro ao buscar Nivel2!</span>');
       }
    },
});


//BUSCA GRUPOS
$('#grupo').on('change', function(){
var id = $('#grupo').val();
$.ajax({
    url : "<?php echo site_url('projeto/ajax_busca_grupos/')?>/" + id,
    type: "POST",
    dataType: "JSON",
    success: function(data)
    {
      if (data.length > 0){    
          var option = '<option>Selecione..</option>';
          $.each(data, function(i, obj){
                option += '<option 
      value="'+obj.id_grp+'">'+obj.grupos+'</option>';
          })
          $('#sub_grupo').html(option).show();
       }else{
           Reset();
           //$('#mensagem').html('<span class="mensagem">Erro ao buscar 
Nivel2!</span>');
       }
    },
});
})

to edit the record use the following ajax:

function edit_projeto(id)
{
save_method = 'update';
$('#form')[0].reset(); // reset form on modals
$('.form-group').removeClass('has-error'); // clear error class
$('.help-block').empty(); // clear error string

//Ajax Load data from ajax
$.ajax({
    url : "<?php echo site_url('projeto/ajax_edit/')?>/" + id,
    type: "GET",
    dataType: "JSON",
    success: function(data)
    {
        console.log(data);
        $('[name="id"]').val(data.id);
        $('[name="owner"]').val(data.owner);
        $('[name="dpo"]').val(data.dpo);
        $('[name="semana"]').val(data.semana);
        $('[name="grupo"]').val(data.grupo);
        $('[name="sub_grupo"]').val(data.sub_grupo);
        $('[name="cod_id"]').val(data.cod_id);
        $('[name="atividade"]').val(data.atividade);
        $('[name="obj_esperado"]').val(data.obj_esperado);
        $('[name="alcancado"]').val(data.alcancado);
        $('[name="conc_esti"]').val(data.conc_esti);
        $('[name="anl_envolvidos"]').val(data.anl_envolvidos);
        $('#modal_form').modal('show'); // show bootstrap modal when complete loaded
        $('.modal-title').text('Editar DPO'); // Set title to Bootstrap modal title

    },
    error: function (jqXHR, textStatus, errorThrown)
    {
        alert('Erro buscando registros do banco');
    }
});
}

And further down the PHP functions

    public function ajax_edit($id)
{
    $data = $this->projeto->get_by_id($id);
    echo json_encode($data);
}

public function ajax_update()
{
    $this->_validate();
    $data = array(
            'owner' => $this->input->post('owner'),
            'dpo' => $this->input->post('dpo'),
            'grupo' => $this->input->post('grupo'),
            'sub_grupo' => $this->input->post('sub_grupo'),
            'cod_id' => $this->input->post('cod_id'),
            'semana' => $this->input->post('semana'),
            'atividade' => $this->input->post('atividade'),
            'obj_esperado' => $this->input->post('obj_esperado'),
            'alcancado' => $this->input->post('alcancado'),
            'conc_esti' => $this->input->post('conc_esti'),
            'anl_envolvidos' => $this->input->post('anl_envolvidos'),
        );
    $this->projeto->update(array('id' => $this->input->post('id')), $data);
    echo json_encode(array("status" => TRUE));
}

Could anyone explain what is wrong ? and what I can do to fix ? Thank you

  • Somebody, give me a boost ?

  • Anyone can help?

No answers

Browser other questions tagged

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