Popular a select PHP with Mysql data of city and state

Asked

Viewed 1,377 times

1

I found some articles by Stack already, however, I tried anyway to make it work and could not. I own a bank with all states and cities of Brazil, which are ok.

Select page

<!-- Select Estado -->
<div class="form-group">
<label class="col-md-4 control-label" for="selectbasic">Estado</label>
<div class="col-md-6">
<select id="estados" name="selectEstado" class="form-control" onchange="getStates();">
<?php
$resultEstado = getEstado($con);
while ($rowEstado = $resultEstado->fetch_assoc()) {
echo "<option value=\"{$rowEstado['nome']}\">";
echo $rowEstado['nome'];
echo "</option>";
}
?>
</select>
</div>
</div>

<!-- Select Cidade -->
<div class="form-group">
<label class="col-md-4 control-label" for="selectbasic">Cidade</label>
<div class="col-md-6">
<select id="cidades" name="selectCidade" class="form-control">
<option value="">Selecione o Estado</option>
</select>
</div>
</div>

At the bottom of this page there is a include "include('footer.php');? > " where is the following script:

<script>
function getStates(){
$.ajax({
url:'lib/getCidades.php',
cache:false,
data:{state:$('#estados').val()},
beforeSend:function(){
// do something here. possibly a loader
},
success:function(response){
// remove the loader here
$('#cidades').html(response);
}
})
}
</script>

And here the file getCities

$resultCidadeBaladas = getCidadeBalada($con);
while ($rowCidade = $resultCidadeBaladas->fetch_assoc()) {
echo "<option value=\"{$rowCidade['nome']}\">";
echo $rowCidade['nome'];
echo "</option>";
}

No matter how hard you try to select a state, it returns no city. Chrome console returns the following:

Uncaught Referenceerror: getStates is not defined

  • 1

    By mistake I think he is not finding the script (see if the path of the footer.php is correct)... you can also do a test by placing the script at the bottom of the select page, just before the </body>... In time, it’s calling jQuery right?

  • I believe that the footer is correct yes, I will make a test soon. Jquery is in the footer too, that’s what I wondered. In footer the script is after Jquery, right? In getCidades you would need to include footer as well?

  • If in the getCidades have only that php script does not need... It is important that jQuery is before the script, otherwise ajax will not work. Open the Chrome inspector, go to Network, give an F5 and see if jQuery is loading right. See if it appears in the list of uploaded files...

  • http://answall.com/questions/99107/listar-estados-cidades-e-bairros-em-formul%C3%A1rio-de-cadastro/99133#99133

No answers

Browser other questions tagged

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