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