Cercando su internet ho trovato un database sql proto da esportare
popolato con tutte le Regioni Province e Comuni d'italia e scaricabile al seguente indirizzo web: http://www.sarnari.net/programmazion...atabase-mysql/
In seguito ho cambiato per esigenza personale i dati delle tabelle da così:
regioni____________ province____________comuni
idregione –>________idregione___________idcomune
nomeregione_______idprovincia –>_______idprovincia
__________________siglaprovincia_______nomecomune
__________________nomeprovincia
a così:
regioni_____________province____________comuni
id –>______________id_regione___________id
regione____________id –>_______________id_provincia
__________________siglaprovincia________comune
__________________provincia
di seguito ho modificato il codice per adattarlo al nuovo database nel seguente modo:
pagina getCity.php
codice:
<?
include('inc/conn.inc.php');
include('inc/class/select.class.php');
$id = $_REQUEST['id'];
//$id = 7;
$obj = new select();
$comuni = $obj->getL3($id);
$Js = '';
for($y=0;$y<count($comuni);$y++){
if($y < (count($comuni)-1)){
$end = ",";
}else{
$end = "";
}
$Js .= $comuni[$y]['id']."-".$comuni[$y]['comune'].$end;
}
echo $Js;
?>
pagina getState.php
codice:
<?
include('inc/conn.inc.php');
include('inc/class/select.class.php');
$id = $_REQUEST['id'];
$obj = new select();
$regioni = $obj->getL1($id);
$Js = '';
for($x=0; $x<count($regioni); $x++){
$province = $obj->getL2($regioni[$x]['id']);
for($y=0;$y<count($province);$y++){
if($y < (count($province)-1)){
$end = ",";
}else{
$end = "";
}
$Js .= $province[$y]['id']."-".$province[$y]['provincia'].$end;
}
}
//echo "text";
echo $Js;
?>
pagina index.php
codice:
<?
include('inc/conn.inc.php');
include('inc/class/select.class.php');
$obj = new select();
$regioni = $obj->getL1();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="it">
<head>
<title>Select dinamiche con dati provenienti da Database</title>
<script type="text/javascript">
var http = createRequestObject();
var where = (navigator.appName == "Microsoft Internet Explorer") ? -1 : null;
function createRequestObject() {
var ro;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
ro = new ActiveXObject("Microsoft.XMLHTTP");
}else{
ro = new XMLHttpRequest();
}
return ro;
}
function addElement(ogg,val,text){
var newOption;
newOption = document.createElement("option");
newOption.value = val;
newOption.text = text;
ogg.add(newOption, where);
}
function removeElement(ogg){
if(ogg.options.length > 0){
while (ogg.options.length) {
ogg.remove(0);
}
}
}
function disabledElement(ogg,val){
document.getElementById(ogg).disabled = val;
}
function loadingState(oggId){
var id = document.getElementById(oggId).value;
var City = document.getElementById('comuni');
// inizializzazione Città
removeElement(City);
addElement(City,'sel','Seleziona una Città:');
disabledElement('comuni',true);
http.open('GET','getState.php?id='+id, true);
http.onreadystatechange = getState;
http.send(null);
}
function loadingCity(oggId){
var id = document.getElementById(oggId).value;
http.open('GET','getCity.php?id='+id, true);
http.onreadystatechange = getCity;
http.send(null);
}
function getState(){
var State = document.getElementById('province');
removeElement(State);
id = document.getElementById('regioni').value;
if(id == 'sel'){
disabledElement('province',true);
addElement(State,'sel','Seleziona prima un Continente:');
}else{
if(http.readyState == 4){
if (http.status == 200) {
var response = http.responseText;
if(response == ''){
disabledElement('province',true);
addElement(State,'sel','Nessuno Stato Presente');
}else{
removeElement(State);
coppia = response.split(',');
max = coppia.length;
addElement(State,'sel','Seleziona uno Stato:');
for(x=0;x<max;x++){
val = coppia[x].split('-');
addElement(State,val[0],val[1]);
}
disabledElement('province',false);
}
}
}else{
addElement(State,'sel','Loading...');
}
}
}
function getCity(){
var City = document.getElementById('comuni');
removeElement(City);
id = document.getElementById('province').value;
if(id == 'sel'){
disabledElement('comuni',true);
addElement(City,'sel','Seleziona una Città:');
}else{
if(http.readyState == 4){
if (http.status == 200) {
var response = http.responseText;
if(response == ''){
disabledElement('comuni',true);
addElement(City,'sel','Nessuna voce');
}else{
removeElement(City);
coppia = response.split(',');
max = coppia.length;
addElement(City,'sel','Seleziona una Città:');
for(x=0;x<max;x++){
val = coppia[x].split('-');
addElement(City,val[0],val[1]);
}
disabledElement('comuni',false);
}
}
}else{
addElement(City,'sel','Loading...');
}
}
}
</script>
</head>
<body>
<form name="form">
<div id="content">
<div id="label1">
Select 1
</div>
<div id="select1">
<select name="regioni" id="regioni" onChange="loadingState('regioni')">
<option value="sel" selected="selected">Seleziona un Continente:</option>
<?
for($x=0;$x<count($regioni);$x++){
?>
<option value="<?=$regioni[$x]['id']?>"><?=$regioni[$x]['regione']?></option>
<?
}
?>
</select>
</div>
<div id="label2">
Select 2
</div>
<div id="select2">
<select name="province" id="province" onChange="loadingCity('province')" disabled>
<option value="sel" selected="selected">Seleziona uno Stato:</option>
</select>
</div>
<div id="label3">
Select 3
</div>
<div id="select3">
<select name="comuni" id="comuni" disabled>
<option value="sel" selected="selected">Seleziona una Città:</option>
</select>
</div>
</div>
</form>
</body>
</html>
pagina getState.php
codice:
<?php
class select{
function select(){
}
function getL1($id=''){
if(!empty($id)){
$sSQL = "SELECT * FROM regioni WHERE id = $id";
}else{
$sSQL = "SELECT * FROM regioni Order by id";
}
$ris = mysql_query($sSQL);
while($row = mysql_fetch_assoc($ris)){
$list[] = array('id' => $row["id"],
'regione' => $row["regione"]);
}
return $list;
}
function getL2($id){
$sSQL = "SELECT * FROM province WHERE id_regione = $id Order By id";
$ris = mysql_query($sSQL);
while ($row = mysql_fetch_assoc($ris)) {
$list[] = array('id' => $row["id"],
'provincia' => $row["provincia"]);
}
return $list;
}
function getL3($id){
$sSQL = "SELECT * FROM comuni WHERE id_provincia = $id Order By id";
$ris = mysql_query($sSQL);
while ($row = mysql_fetch_assoc($ris)) {
$list[] = array('id' => $row["id"],
'comune' => $row["comune"]);
}
return $list;
}
}
?>
Ciao, ciao...