Solved

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

Posted on 2013-11-26
7
985 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 109

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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article discusses how to create an extensible mechanism for linked drop downs.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
The viewer will learn how to count occurrences of each item in an array.

770 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