show user by registered registration Codeigniter

0

Hello, I would really like your help, because I do not work in the area, but I really like to learn, I started messing with codeigniter, I'm making a system for my work people, today everything is done in excel. The system is to manage associates, I have a table of enrollments and name of all employees; My problem is when I type the registration, I can not pull the person's name.

In my view I have:

<div class="col-xs-3 form-group">
    <label>Matrícula</label>
    <input style="text-transform:uppercase" class="form-control" type="text" id="matricula" name="matricula" placeholder="Nº matrícula"/>
</div>
<div class="col-xs-6 form-group">
    <label for="nome">Nome</label>
    <input style="text-transform:uppercase"  class="form-control" type="text" id="nome" name="nome" placeholder="Digite a matricula" required/>
</div>

At the end of my view is the script with an AJAX request

$("input[name='matricula']").blur(function()
{

    event.preventDefault();
    /*
    var matricula = $('#matricula').val();
    */
    var nome = $("input[name='nome']");
    nome.val('Carregando...');

    $.ajax({
        url: baseurl+ 'gesind/pesquisa_mat',
        type: 'POST',
        data: {
            matricula: $('#matricula').val()
        },

        success: function(){
            var nome = data['matricula'];
        },
        error: function(){
            alert("erro");  
        }

    });

});

In my controller I have

public function pesquisa_mat()
    {

        if (!$this->input->is_ajax_request()) 
        { 
            exit('no valid req.'); 
        }
        $usr['matricula'] = $this->input->post('matricula');

        $this->load->model("gesind_model");
        $result = $this->gesind_model->pesquisa_matricula($usr);
        if($result)
        {
            echo $result;
        }
        else
        {
            echo false;
        }

}

and in the Model I have the following code

public function pesquisa_matricula($usr) //checks ajax requests
{   
    $this->db->where('matricula',$usr);
    $query=$this->db->get("matriculas");
    if($query->num_rows()>0)
    {
        return $query->result();

    }
    else
    {
        return false;
    }

}

If anyone can help me, I am grateful.

    
asked by anonymous 22.01.2018 / 17:57

1 answer

0

Below is the code

In the Model, we are selecting everything from the matriculas table where the matricula column is equal to a parameter we are passing to the function. The method will return FALSE if it does not find at least one match in db, otherwise it returns the first line found (I'm assuming there is only one name per registration)

public function pesquisa_matricula($matricula) {
    $this->db->select('*');
    $this->db->where('matricula',$matricula);
    $query = $this->db->get('matriculas');
    if ($query->num_rows() > 0) {
        return $query->row();
    }else{
        return FALSE;
    }
}

In your Controller, we are getting the value sent by the ajax request and placing it inside the $matricula variable and sending this value to our Model searching in db. For the data return I used the output class. In it we are saying that we will return a JSON and construct the same. JSON has 3 fields

  • data - Contains the name of the user returned from the Model
  • Error - Informs if you hear any errors in the query
  • merro - Error message occurred
  • This way of returning the data is easier on the front end.

    public function pesquisa_mat()  {
        if (!$this->input->is_ajax_request()) {
            exit('no valid req.');
        }
        $matricula = $this->input->post('matricula');
    
        $this->load->model("gesind_model");
        $result = $this->gesind_model->pesquisa_matricula($matricula);
        if($result) {
            $this->output
            ->set_content_type('application/json')
            ->set_output(json_encode(array( 'dados' => $result->nome,
                                            'erro'  => FALSE,
                                            'merro'   => ''
            )));
        }
        else {
            $this->output
            ->set_content_type('application/json')
            ->set_output(json_encode(array( 'dados' => '',
                                            'erro'  => TRUE,
                                            'merro'   => 'Usuário não encontrado'
            )));
        }
    }
    

    The Javascript to make this request follows below. Note we are testing in the callback function if my return exists any error and shows the due message to the user. If the search did not return any errors the script takes the returned value and plays within the nome field of the form.

    $("input[name='matricula']").blur(function() {
        event.preventDefault();
        var matricula = $('#matricula').val();
    
        $.ajax({
            url: baseurl+ 'gesind/pesquisa_mat',
            type: 'POST',
            dataType: 'json',
            data: {
                matricula: $('#matricula').val()
            },
            success: function(data){
                if (data.erro) {
                    $('#nome').val(data.dados);
                    alert(data.merro);
                }else{
                    $('#nome').val(data.dados);
                }
            },
            error: function(){
                alert("erro");
            }
        });
    });
    

    To check everything I created the table and everything is working as expected. If you continue giving error leave a comment to try to solve.

        
    22.01.2018 / 18:47