Ciao Vales, sto impazzendo a collegare i miei dropdown alle tabelle che ho creato del mio database seguendo le funzioni sql che hai postato tu.
CREATE TABLE `regioni` (
`id` int(11) unsigned NOT NULL,
`regione` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
CREATE TABLE `province` (
`id` int(10) unsigned NOT NULL,
`id_regione` int(10) unsigned NOT NULL,
`provincia` text NOT NULL,
`sigla` varchar(2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
CREATE TABLE `comuni` (
`id` int(10) unsigned NOT NULL auto_increment,
`id_regione` int(10) unsigned NOT NULL,
`id_provincia` int(10) unsigned NOT NULL,
`comune` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
Quindi ho le rispettive tabelle nel DB.
Inserisco i seguenti codici Html e Javascript ma non funziona, mi da i campi dropdown vuoti.
HTML:
<?php
$db =& JFactory::getDBO();
$query = "
SELECT `id`, `regione`
FROM `#__regioni` ORDER BY `regione`";
$db->setQuery($query);
$nomes = $db->loadAssocList();
$option_reg="";
foreach ( $nomes as $v ) {
$selected="";
$id = $v['id'];
$regione = $v['regione'];
if ($id==JRequest::getVar('regione',0,'GET','INT')) {
$selected="selected=\"selected\"";
}
$option_reg.="<option value=\"$id\" $selected>$regione</option>\n";
}
$query = "
SELECT `id`, `provincia`
FROM `#__province` WHERE `id_regione`=".JRequest::getVar('regione',0,'GET','INT')." ORDER BY `provincia`";
$db->setQuery($query);
$nomes = $db->loadAssocList();
$option_pro="";
foreach ( $nomes as $v ) {
$selected="";
$id = $v['id'];
$province = $v['provincia'];
if ($id==JRequest::getVar('provincia',0,'GET','INT')) {
$selected="selected='selected'";
}
$option_pro.="<option value=\"$id\" $selected>$province</option>\n";
}
$query = "
SELECT `id`, `comune`
FROM `#__comuni` WHERE `id_provincia`=".JRequest::getVar('provincia',0,'GET','INT')." ORDER BY `comune`";
$db->setQuery($query);
$nomes = $db->loadAssocList();
$option_com="";
foreach ( $nomes as $v ) {
$id = $v['id'];
$comune = $v['comune'];
$option_com.="<option value=\"$id\" $selected>$comune</option>\n";
}
?>
<div class="form_item">
<div class="form_element cf_dropdown">
<div style="clear:both;"><label class="cf_label">Regioni</label>
<select id="regione" name="regione" onchange="province(this.form);" class="cf_inputbox validate-selection" ><option value="" selected="selected">Seleziona</option>
<?php echo $option_reg; ?>
</select>
</div>
</div>
<div class="cfclear"> </div>
</div>
<div class="form_item">
<div class="form_element cf_dropdown">
<div style="clear:both;"><label class="cf_label">Provincie</label>
<select id="provincia" name="provincia" onchange="reload(this.form);" class="cf_inputbox validate-selection" ><option value="" selected="selected">Seleziona</option>
<?php echo $option_pro; ?>
</select>
</div>
</div>
<div class="cfclear"> </div>
</div>
<div class="form_item">
<div class="form_element cf_dropdown">
<div style="clear:both;"><label class="cf_label">Comuni</label>
<select id="comune" name="comune" class="cf_inputbox validate-selection" ><option value="" selected="selected">Seleziona</option>
<?php echo $option_com; ?>
</select>
</div>
</div>
<div class="cfclear"> </div>
</div>
Javascript:
function province(form)
{
var reg=form.regione.options[form.regione.options.selectedIndex].value;
self.location='index.php?option=com_chronocontact&chronoformname=reg_prov_com®ione=' + reg ;
}
function reload(form)
{
var prov=form.provincia.options[form.provincia.options.selectedIndex].value;
var reg=form.regione.options[form.regione.options.selectedIndex].value;
self.location='index.php?option=com_chronocontact&chronoformname=reg_prov_com&provincia=' + prov + '®ione=' + reg;
}
Modifico il nome del form (chronoformname con nome_mio_form) ma niente da fare.
Dove sbaglio?