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