Automatically fill a text box with a database value

1

I have a form with 5 fields, and I need the location field to be populated with a database value corresponding to the Postal Code field that was previously filled in. I'm using the codeigniter framework.

Follow my Form:

        <form role="form" id="formularioContacto" method="post">
          <div class="form-group">
            <label for="pNome">Primeiro e Último Nome:</label>
            <input type="text" class="form-control" name="puNome" id="puNome" required>
            <span id="erro-puNome"></span>
          </div>              
          <div class="form-group">
            <label for="cPostal">Código Postal:</label>
            <input type="text" class="form-control" name="cPostal" id="cPostal" required>
            <span id="erro-cPostal"></span>
          </div>
          <div class="form-group">
            <label for="localidade">Localidade:</label>
            <input type="text" class="form-control" name="localidade" id="localidade" required>
            <span id="erro-localidade"></span>
          </div>
          <div class="form-group">
            <label for="telefone">Telefone:</label>
            <input type="text" class="form-control" id="telefone" name="telefone" required>
            <span id="erro-telefone"></span>
          </div>
          <div class="form-group">
            <label for="email">Email:</label>
            <input type="email" class="form-control" id="email" required>
            <span id="erro-email"></span>
          </div>      
          <button type="submit" id="submeterForm1"  class="btn btn-default">Enviar</button>
    </form>

Controller that returns the zip code location:

    public function localidade(){

    $codPostal = $this->input->get('term', TRUE);

    $this->load->model('moradas');
    $codPostais = $this->moradas->localidade($codPostal);

    echo json_encode($codPostais);

}

Corresponding Model:

public function localidade($codpostal)
{  
    $this->db->where('codpostal', $codpostal);
    $query = $this->db->get('localidades');       

    $resultado = $query->row();
    return $resultado;
}

From jquery I have only developed the code that detects when the input Postal Code is no longer selected, because it would be at that time that the input Locality should be filled:

    $('#cPostal').focusout(function(){

});

Any light on how to do it? Thanks

    
asked by anonymous 27.07.2016 / 17:51

1 answer

3

I have already solved I used the following ajax code:

$(document).ready(function() {  

    $('#cPostal').focusout(function(){
    localidade();
});

});  


 function localidade(){  

    var cPostal = $('#cPostal').val().trim();  

    //use ajax to run the check  
    $.post("http://localhost/formularioteste/index.php/welcome/localidade", { cPostal: cPostal },  
        function(result){ 

            $("#localidade").val(result[0].localidade);
            console.log(result.localidade); 

    });  

} 

And in the controller I changed the get method to post, now just take the array in json and fill in the text box

    
27.07.2016 / 18:15