• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
3 DEPENDENT DROP DOWN MENU WITH JQUERY, PHP AND MYSQL

#1
[eluser]cbaldinu[/eluser]
Hi all,

i have a problem creating 3 dependent drop down menu with jquery.

This 3 menus are state, provinces and cities.

I think i'll post the code, so you can understand me as good as possible!!! :p

This is from the view and its the html:

Code:
<tr>
        <td>Regione</td>
        <td>&lt;?php echo form_dropdown_from_db('regione','SELECT id, nome FROM regioni ORDER BY nome ASC','','class="required" id="regione"','Seleziona ...');?&gt;</td>
        <td>Prov</td>
        <td><div id="provincia"></div></td>
    </tr>
    <tr>
        <td>Citt&agrave;</td>
        <td><div id="cities">&lt;?php $options = array('0'=>'Seleziona ...');
                            echo form_dropdown('citta',$options,'','class="required" id="citta"');?&gt;</div></td>
        <td>Telefono</td>
        <td>&lt;?php echo form_input(array('name'=>'telefono', 'class'=>'required')); ?&gt;</td>
    </tr>


** The form_dropdown_from_db it's a custom function i've took and it works perfectly!!!

(I know writing a query in a view it's not the best way!! LoL)

This is the jquery code:

Code:
$(document).ready(function() {

      
    $("#regione").change(
                function(){
                
                $.post(
                        
                            "contacts/regione",{
                            comune : $("#regione").val()                          
                            },
                            function(dati){
                            $("#provincia").html(dati);                            
                            });
                
                });
                
                
                
       $("#prov").change(
                function(){
                
                $.post(
                        
                            "contacts/provincia",{
                            prov : $("#prov").val()                          
                            },
                            function(dati){
                            $("#cities").html(dati);                            
                            });
                
                });
    
    
});


This is the controller:

Code:
&lt;?php  

class Contacts extends CI_Controller {
    
    public function index(){
        
        $this->load->model('contatti');
        
        $result = $this->contatti->lista();
        
        if($result!=false){
            foreach($result as $list){
                $data[] = $list;
            }
            $data['title'] = "LISTA CONTATTI";
            $this->load->view('contatti',$data);
        }
        
    }


    public function regione(){
        
        $prov = "<select name=\"prov\" id=\"prov\" class=\"required\">
        <option value=\"0\">Seleziona ...</option>";
        
        
        $this->load->model('contatti');
        $q = $this->contatti->getProv($_POST['comune']);
        if($q!=false){
            foreach($q as $a){
            
                $prov.="<option value=\"".$a['codice']."\">".$a['nome']."</option>";
            
            }
        
        
        $prov.="</select>";
        
        
        
        }  echo $prov;
    
    }
    
    
    public function provincia(){        
        
        $cities = "<select name=\"citta\" id=\"citta\" class=\"required\">
        <option value=\"0\">Seleziona ...</option>";
        
        
        $this->load->model('contatti');
        $q = $this->contatti->getCitta($_POST['prov']);
        if($q!=false){
            foreach($q as $a){
            
                $cities.="<option value=\"".$a['id']."\">".$a['nome']."</option>";
            
            }
        
        
        $cities.="</select>";
        
        
        
        }  echo $cities;
        
    
    }

}
?&gt;


And this is the model:

Code:
&lt;?php

class Contatti extends CI_Model {
    
    public function getProv($comune){
        
            $query = $this->db->query("SELECT province.*
                               FROM province
                            WHERE
                            province.codice_regione=".$comune."
                            ORDER BY nome ASC");
        
        if( $query->num_rows() > 0 ){
            
            foreach($query->result_array() as $risultato){
                
                $a[] = $risultato;
                
            }
            return $a;
            
        }
        return false;
        
    }
    
    
    public function getCitta($provincia){
        
            $query = $this->db->query("SELECT comuni.*
                           FROM comuni
                            WHERE
                            comuni.codice_provincia=".$provincia."
                            ORDER BY nome ASC");
        
        if( $query->num_rows() > 0 ){
            
            foreach($query->result_array() as $risultato){
                
                $a[] = $risultato;
                
            }
            return $a;
            
        }
        return false;
        
    }
    
    
}

?&gt;


The problem is when the code goes to load cities. While i click on the Regione (that's the region) field, the provinces are loaded correctly. But when i click on a province, the cities are not loaded.

Can anyone help me with this trouble?? I think that the code it's correct, dont you think?

Thanks in advance to anyone!!!


Cristian


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.