Solved

Dynamic dropdowns (EX: Dynamic Dependant DropDown List: US States & Counties)

Posted on 2013-11-26
7
958 Views
Last Modified: 2014-01-23
I need to build a form that has three select and need to change the data of dropdowns based on a previous dropdown selection.
Something like Dynamic Dependant DropDown List: US States & Counties, using Zend Form

Can you help me with an example or a tutorial?

Thanks,

Lucilia Coelho
0
Comment
Question by:luciliacoelho
  • 4
  • 2
7 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39678543
Is your question specific to Zend Form?  Because if you just make a Google search for Dynamic Dependent DropDown List: US States & Counties you get a great number of responsive answers (it's apparently a common question).
Ex: http://www.bitrepository.com/dynamic-dependant-dropdown-list-us-states-counties.html
0
 
LVL 1

Expert Comment

by:gwnet
ID: 39679413
public function init(){
$states = $this->db->select()->from('dictionary_states', array('id_states', 'name_states'))->query()->fetchAll(\PDO::FETCH_KEY_PAIR);

        $this->addElement('select', 'states', array(
            'label'                  => 'Country:',
            'value'            => '5', // default states
            'required'   => $this->default_policy,
             'onclick' => 'getCounties(this)' // JavaScritp function - get Ajax Counties
            'MultiOptions'      =>array($states)
        ));

         // this fill in JS or GET FORM IN JS AJAX  when select State

/*
$counties = $this->db->select()->from('dictionary_counties', array('id_county', 'name_county'))
->where('id_states' = ? )
->query()fetchAll(\PDO::FETCH_KEY_PAIR);
*/

        $this->addElement('select', 'county', array(
            'label'                  => 'County:',
            'value'            => '0', // default county
            'MultiOptions'      =>array(0='select county') // $counties
        ));
}
0
 

Author Comment

by:luciliacoelho
ID: 39680314
Ray_Paseur,

Yes, specific to Zend Form, my problem is by working with zend+ajax
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:luciliacoelho
ID: 39680324
Thanks gwnet, but can you show me javascript (Ajax) and where do I put the script.
I'm new to working with zend.

Thanks
0
 
LVL 1

Expert Comment

by:gwnet
ID: 39682012
EXAMPLE VIEW ( locate eq views/scripts/index/index.phtml  )
add:
 <script type="text/JavaScript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script> 

<script type="text/javascript">
function getCounties(e) {
    // set image loaader ... or style
    $(self).html('').addClass('t_a_ui_helper_loading');
    // post data to your controller/action  
    var jqxhr = $.ajax({
        url: "controller/getcounties", // !!! your controller and action 
        global: false,
        type: "POST",
        data: e.value, // id_state
        dataType: "html",
        async: true
    })
            .success(function(response) {
                // eg return from controller json object
                var res = $.parseJSON(response);

                //!!!  prepare you html and inner as options of select !!!!

            })
            .error(function() {
                alert('ERROR GET DATA COUNTIES')
            });
    jqxhr.complete(function(response) {
        alert("complete");
    });
}
</script>
<?php echo $this->form; ?>

Open in new window

0
 
LVL 1

Accepted Solution

by:
gwnet earned 500 total points
ID: 39682017
EXAMPLE FORM :

<?php

class Application_Form_State extends Zend_Form {

    public function isValid($data)
    {
        // call parent, which will populate newly created elements.
        return parent::isValid($data);
    }

    public function init()
    {

        //$states = $this->db->select()->from('dictionary_states', array('id_states', 'name_states'))->query()->fetchAll(\PDO::FETCH_KEY_PAIR);
        $states = array(0 => 'select state', 1 => 'Alabama');
        $this->addElement('select', 'states', array(
            'label' => 'States:',
            'value' => '0', // default states
            'onclick' => 'getCounties(this)', // JavaScritp function - get Ajax Counties
            'MultiOptions' => $states
        ));

        $counties = $this->getCounties(0);
        $this->addElement('select', 'county', array(
            'label' => 'County:',
            'value' => '0', // default county
            'MultiOptions' => $counties // $counties
        ));

        $this->addElement('submit', 'submit', array(
            'required' => false,
            'ignore' => true,
            'label' => 'Add',
        ));
    }

    public function getCounties($idState = 0)
    {
        /*
          $counties = $this->db->select()->from('dictionary_counties', array('id_county', 'name_county'))
          ->where('id_states' = ? )
          ->query()fetchAll(\PDO::FETCH_KEY_PAIR);
         */

        // this is example - use db to doo this
        if ($idState === 1)
        {
            $counties = array(0 => 'select county', 1 => 'Autauga County', 2 => 'Baldwin County', 'Barbour County'); // eg .
        }
        else
        {
            $counties = array(0 => 'select county', 1 => 'Autauga County', 2 => 'Baldwin County', 3 => 'Barbour County', 4 => 'Bibb County', 5 => 'and more ....'); // eg .
        }
        return $counties;
    }

}

Open in new window

0
 
LVL 1

Expert Comment

by:gwnet
ID: 39682028
I do not do anything, the rest I leave to you ;)

Add the controller and complete JS.

Regards
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now