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
Solved

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

Posted on 2013-11-26
7
992 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

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
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
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
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

856 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