6
I’m riding a webapp with the Jquery Mobile, PHP and AJAX framework.
In this app I have a form with selects of ESTADO
and CIDADE
, was made an AJAX that when selects the ESTADO
, carries the CIDADES
according to STATE.
The problem I’m having, and that AJAX only loads the first time, and then no more after selecting the ESTADO
for the second time or more.
<script type="text/javascript">
$(document).ready(function(){
$('#estados').change(function(){
alert ('carrega cidade');
$('#cidades').load('cidades.php?estado='+$('#estados').val() );
});
});
</script>
<select name="estados" id="estados">
<option value="0">ESTADO</option>
<?php
mysql_connect('localhost','root','');
mysql_selectdb('proxima-corrida');
$result = mysql_query("select * from proxc_estado");
while($row = mysql_fetch_array($result) ){
echo "<option value='".$row['id_estado']."'>".$row['estado']."</option>";
}
?>
</select>
<select name="cidades" id="cidades">
<option value="0">Escolha um estado</option>
</select>
<?php
$idestado = $_GET['estado'];
mysql_connect('localhost','root','');
mysql_selectdb('proxima-corrida');
$result = mysql_query("SELECT * FROM proxc_cidade WHERE estado_cidade = ".$idestado);
while($row = mysql_fetch_array($result) ){
echo "<option value='".$row['id_cidade']."'>".$row['cidade']."</option>";
}
?>
You can put
console.log('log: ', this.value);
on the first line within$('#estados').change(function(){
and check if this is called when you change status again?– Sergio
Beware of injection of SQL. Because if I use firebug or similar tool to place an element
<option value="5; DELETE FROM proxc_cidade; --">pwned</option>
in the combobox and select it, its application was already.– Victor Stafusa
ok!! I’m still in homologation.. I won’t let the SQL injection @Victor Stafusa go
– Ana Paula Moraes
and on the console.log @Sergio is returning UNDEFINED
– Ana Paula Moraes
@Anapaulamoraes 2 questions: 1- the console appears each time the select changes? 2- in HTML the value of these options is right or the value is
undefined
?– Sergio
then @Sergio, 1- on the console appears yes each time it changes. 2- and in the HTML appears the VALUE of the option, is not appearing more Undefined. Thus, AJAX comes to load the right states and cities. An example, if I select PARANA it loads the CITIES; When I select a second STATE it already stops loading the CITIES of that second selected STATE. I’m guessing it’s some conflict with the Jquery Mobile framework
– Ana Paula Moraes
@Anapaulamoraes can put in question a link to the Pastebin with the rendered HTML of the page?
– Sergio
@Sergio sorry, I don’t understand. I can send the link by comment?!
– Ana Paula Moraes
@Anapaulamoraes yes can be.
– Sergio
@Please follow http://www.domadigital.com/proxima-corrida/
.
– Ana Paula Moraes
@Anapaulamoraes are loading jQuery 1.10 and then 1.3. You can take version 1.3, it will only cause problems...
– Sergio
http://answall.com/questions/99107/listar-estados-cidades-e-bairros-em-formul%C3%A1rio-de-cadastro/99133#99133
– marcusagm