?
Solved

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

Posted on 2013-11-26
7
Medium Priority
?
1,029 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 111

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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

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 2000 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

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

765 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