Hello everyone,

The age old question... I have a simple page with two select boxes and
want the second one to populate according to what was selected in the
first one. So, in steps the ajax helper within cake. I have searched
and searched around the web and found several articles and tutorials
on the subject but many of them are 1.1 and outdated. I feel like I am
so close to getting it to work but can't figure out where I am going
wrong. The first select box is populating but the second is not. So,
I'm turning to the forums :) My setup is really simple, just two
databases, three entries in each. So obviously two models and
controllers and then the views. I'll include the necessary code
snippets for analysis. Any help would be so so appreciated, thanks! It
is worth noting that most of the code was taken off of the blog
http://www.devmoz.com/blog/2007/04/04/cakephp-update-a-select-box-using-ajax/

FIRST, THE TABLES

--
-- Table structure for table `facilities`
--

CREATE TABLE IF NOT EXISTS `facilities` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

--
-- Dumping data for table `facilities`
--

INSERT INTO `facilities` (`id`, `name`) VALUES
(1, 'Atlanta'),
(2, 'Nashville'),
(3, 'Charlotte');

--
-- Table structure for table `trucks`
--

CREATE TABLE IF NOT EXISTS `trucks` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `facility_id` int(11) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `facility_id` (`facility_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

--
-- Dumping data for table `trucks`
--

INSERT INTO `trucks` (`id`, `name`, `facility_id`) VALUES
(1, 'atl', 1),
(2, 'nsh', 2),
(3, 'cnc', 3);

----------------------------------------------------------------------------------------------------------------
NOW THE MODELS

<?php

        class Facility extends AppModel {

                var $name = 'Facility';

        }

?>

<?php

        class Truck extends AppModel {

                var $name = 'Truck';
                var $belongsTo = array('Facility' => array('className' =>
'Facility', 'foreignKey' => 'facility_id'));

        }

?>

----------------------------------------------------------------------------------------------------------------
THE CONTROLLERS

<?php

        class FacilitiesController extends AppController {

                var $name = 'Facilities';
                var $helpers = array('Html', 'Form', 'Javascript', 'Ajax');
                var $components = array('RequestHandler');

        }

?>

<?php

        class TrucksController extends AppController {

                var $name = 'Trucks';
                var $helpers = array('Html', 'Form', 'Javascript', 'Ajax');
                var $components = array('RequestHandler');

                function index() {

                        $this->set('facilities', 
$this->Truck->Facility->find('list'));

                }

                function update_select() {

                        if (!empty($this->data['Facility']['id'])) {

                                $fac_id = $this->data['Facility']['id'];
                                $options = $this->Truck->find('list', 
array('conditions' => array
('Truck.facility_id' => $fac_id)));
                                $this->set('options', $options);

                        }

                }

        }

?>

----------------------------------------------------------------------------------------------------------------
FINALLY, THE VIEWS

//trucks/index.ctp
<?php

    echo $form->select('Facility.id', array('options' => $facilities),
null, array('id' => 'facilities'));
    echo $form->select('Truck.id', array(), null, array('id' =>
'trucks'));

    $options = array('url' => 'update_select', 'update' => 'trucks');
    echo $ajax->observeField('facilities', $options);

?>

//trucks/update_select.ctp
<?php

        if (!empty($options)) {

        foreach ($options as $k => $v) {

                echo "<option value='$k'>$v</option>";

        }

    }

?>


Sorry for such a long post, thanks again for taking the time to view
it and any feedback you might have.

-Regards
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to