Welcome Guest, Not a member yet? Register   Sign In
dropdownlist depending with JQuery Framework
#1

[eluser]Unknown[/eluser]
I need a dropdownlist Depending province -> city, that I created with jquery,I wrote a controller and a view of a model running perfectly in version 1.7.2 of CodeIgniter, but does not work in version 2.0.3 I do not understand why it works .... is loaded only the province and the town is not loaded...sorry for my poor English ... and thanks

this is the controller
Code:
<?php

class Tenda extends CI_Controller
{

        function  __construct()
    {
        parent::__construct();

        $this->load->helper('form');
        $this->load->model('tenda_model');
    }


    function index()
    {
        $data['provincie']=$this->tenda_model->get_provincie();
        
        $this->load->view('tenda_view', $data);
    }

    function get_comuni($provincia)
    {
         header('Content-Type: application/x-json; charset=utf-8');
        echo json_encode($this->tenda_model->get_comuni($provincia));
    }
}


this is the model
Code:
<?php

class Tenda_model extends CI_Model
{

    function  __construct() {
        parent::__construct();
    }

    function get_provincie()
    {
        $this->db->select('id_provincia, nome_provincia');
        $query =$this->db->get('provincie');

        $provincie = array();

        if ($query->result())
                {
                    foreach ($query->result() as $provincia)
                            {
                                $provincie[$provincia->id_provincia] = $provincia->nome_provincia;
                            }
                     return $provincie;
                }
                else
                    {
                      return FALSE;
                    }
      } //chiusura get_provincie
      
      function get_comuni($provincia=NULL)
      {
          $this->db->select('id_comune, nome_comune');
          if ($provincia)
          {
              $this->db->where('id_provincia', $provincia);
          }

          $query = $this->db->get('comuni');

          $comuni =array();

          if($query->result())
                  {
                        foreach ($query->result() as $comune)
                        {
                            $comuni[$comune->id_comune] = $comune->nome_comune;
                        }
                        return $comuni;
                  }
                  else
                  {
                      return FALSE;
                  }

      } //chiusura get_comuni
    } //chiusura file


?>

this is the view
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
&lt;html&gt;
    &lt;head&gt;
        &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
        &lt;title&gt;tenda&lt;/title&gt;
        &lt;style&gt;
        #tenda
        {
            display: block;
}
        &lt;/style&gt;
        [removed][removed]
        [removed]
    $(document).ready(function(){      
        $('#provincia').change( function()
        {

            var id_provincia = $('#provincia').val();
                $.ajax(
                {
                    type: "POST",
                    url:"tenda/get_comuni/"+id_provincia,
                    success: function(comuni)
                    {
                                             $('#comuni').empty();
                        $.each(comuni,function(id_comune, nome_comune)
                            {
                                var opt = $('<option />');
                                opt.val(id_comune);
                                opt.text(nome_comune);
                                $('#comuni').append(opt);
                            });

                                                        
                    }
                });
        });
    });
[removed]
    &lt;/head&gt;
&lt;body&gt;
    &lt;?php $comuni['#'] = 'seleziona'; ?&gt;
    <div id="tenda">
                <label for="provincia">provincia: </label>&lt;?php echo form_dropdown('id_provincia', $provincie, '#', 'id="provincia"'); ?&gt;
              
                <label for="comune">comune: </label>&lt;?php echo form_dropdown('id_comune', $comuni, '#', 'id="comuni"'); ?&gt;<br />
    </div>


&lt;/body
&lt;/html>
#2

[eluser]Soltic[/eluser]
Hi, i tested this depending dropdown and it works perfectly. But in my case i gave 3 depending dropdown with a structure like this:

Code:
CREATE TABLE `Region` (
  `Region_ID` int(11) NOT NULL auto_increment,
  `RegionNom` varchar(255) default NULL,
  `AbbreviationRegion` varchar(2) NOT NULL,
  `Superficie` int(11) NOT NULL,
  PRIMARY KEY  (`Region_ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=18 ;


CREATE TABLE `DistrictSanitaire` (
  `District_ID` int(11) NOT NULL auto_increment,
  `DistrictNom` varchar(255) NOT NULL,
  `Region_ID` int(11) NOT NULL,
  PRIMARY KEY  (`District_ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=74 ;


CREATE TABLE `Institution` (
  `Institution_ID` char(16) NOT NULL,
  `InstitutionNom` varchar(255) default NULL,
  `District_ID` int(11) NOT NULL,
   PRIMARY KEY  (`Institution_ID`),
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

How do i process do to make three depending dropdown with Region,District,Institution.

Thanks
#3

[eluser]Unknown[/eluser]
thanks it help me well...


:-) :-) :-)




Theme © iAndrew 2016 - Forum software by © MyBB