Automatically populate a text box with a database value

Asked

Viewed 281 times

1

I have a form with 5 fields, and I need the local field to be filled with a value of the database corresponding to the field Postal Code 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 locality referring to the postcode:

    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;
}

De jquery I have only developed the code that detects when the Postcode input is no longer selected, as it would be at that time that the Locale input should be filled in:

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

});

Any light on how to do it? Thank you

1 answer

3

I’ve 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 json array and fill in the text box

Browser other questions tagged

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