State Combobox - PHP city

Asked

Viewed 1,756 times

1

This is correctly filling the states combobox. How do I proceed now, when selecting the state to combobox of cities referring to the selected state is filled? How to implement the "chargingCities()"?

    <?
	
 	//seleciona todas as cidades quando o pais nao for o Brasil
	$sql = "select distinct loc_cidade, id_local from local where id_pais = '" . $id_pais . "' and loc_cidade <> '' "; 
	$sql .= " and loc_tipo = 'C' ";
	$sql .= " order by loc_cidade";
	$local = Consulta($sql, $conexao); 
	
	$rs = mysql_query("select distinct loc_uf from local where id_pais = '27' ORDER BY loc_uf ASC");
	
	
?>


<select name="estado" id="estado" onChange="carregaCidades()">
	<option selected value="estado">Estado</option>
    <?php while($reg = mysql_fetch_object($rs)): ?>
        <option value="<?php echo $reg->id_local ?>"><?php echo $reg->loc_uf ?></option>
    <?php endwhile; ?>
    </select>
        
    <div id="load_cidades">
    <select name="cidade" id="cidade">
    	<option value="">Aguardando o estado</option>
    </select>
    </div>
    

  • Here you can try. http://answall.com/questions/87411/selectr-o-pa%C3%Ads-e-traz-o-estado/87417#87417

  • You need to use ajax, on onchange() of the state combo create a function that makes an ajax call to a php file, then take the return and treat. is more or less the idea.

1 answer

2

In your file of scripts jQuery, add a function to take care of the event change

jQuery("#estado").on('change',function(){
  var estadoSelecionado = jQuery( this ).val();
  //e aqui vc executa o restante do código para carregar as cidades
  //chamando por ajax a página que carrega a cidade...
  //e vc adiciona a estadoSelecionado como parâmetro
});

That way you don’t hold the function in the HTML tag. A good example is in this link

Browser other questions tagged

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