function recuperaRacas($idAnimal) {
$idAnimal = strip_tags($idAnimal);
$query = "select raca_id 'idRaca', raca 'raca' from raca_nimal where id_nimal = " .$idAnimal. " order by raca;";
$queryBuscaRaca = $this->conexaoMysql->query($query);
$queryBuscaRaca->execute();
$listaRacas = array();
while ($row = $queryBuscaRaca->fetch(PDO::FETCH_ASSOC)) {
$listaRacas[] = array('idRaca' => utf8_encode($row["idRaca"]),
'raca' => utf8_encode($row["raca"]));
}
return $listaRacas;
}
// Model
// start controller
case 'buscaRaca':
$idAnimal = strip_tags($_REQUEST['idAnimal']);
$listaRacas = $Doacao->recuperaRacas($idAnimal);
$html = "";
foreach ($listaRacas as $raca) {
$html .= "<option value='" . $raca['idRaca'] . "'>" . $raca['raca'] . "</option>";
}
echo $html;
break;
}
// end controller
// start view
<div class="col-xs-8">
<div class="form-group">
<label>Qual a raça?</label>
<select class="" id="raca" name="raca">
<option value="">Selecione uma opcao</option>
<?php foreach ($listaRacas as $raca): ?>
<?php if ($raca['idRaca'] == $raca['idRaca']): ?>
<option selected="selected" value='<?= $raca['idRaca'] ?>'><?= $raca['raca'] ?></option>";
<?php else: ?>
<option value='<?= $raca['idRaca'] ?>'><?= $raca['raca'] ?></option>";
<?php endif ?>
<?php endforeach ?>
</select>
</div>
</div>
$(document).ready(function () {
if ($('#tipo_nimal').val() != "") {
buscarRacas();
}
});
function buscarRacas() {
var idAnimal = $('#tipo_nimal').val();
$.ajax({
url: '../Controller/DoacaoController.php',
type: 'POST',
dataType: 'html',
data: {action: 'buscaRaca', idAnimal: idAnimal},
success: function (data) {
$('#raca').empty().append(data);
}
});
}
//
I wanted to remember that I am using the same codes for the 4 pages, can that give anything? or should the popular select us in 4?