[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2013-11-26
7
Medium Priority
?
1,046 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

650 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