I am new to cakephp, I am confused and scraching my head for more than
a day, all I want is a dynamic select box.... I have a add.ctp file
and controller, and there are 2 select boxes, my requirement is if I
select the first select box called category, the second select box
called subcategory should be populated..
I tried to do by looking in the code posted at
http://j4vk.com/wordpress/2008/08/07/cakephp-dynamic-select-boxes-ajax/
and
http://web.archive.org/web/20070501122029/www.devmoz.com/blog/2007/04/04/cakephp-update-a-select-box-using-ajax/
 but with little success....

I have downloaded, prototype.js and this is what the controller looks
like

<?php
class BooksController extends AppController{
        var $name = 'Books';
        var $uses = array('Book','Author','Cat','Subcat');
        var $helpers = array('Html','Javascript','Ajax','Form');
        var $components = array('RequestHandler');
        //var $scaffold;
        function index(){
                $this->Book->recursive=1;
                $this->set('books',$this->Book->find('all'));
                $this->set('authors',$this->Author->find('all'));
        }
        function add(){

                        if (!empty($this->data)) {

                                        $this->Book->create();
                                        //for autopopulating select boxes
                                if(!empty($this->data['Cat']['id'])) {
                                echo ('<script language="javascript" 
>alert("hello Me from
update_select()"); </script>');
                            $catid = (int)$this->data['Cat']['id'];
                                $data=$this->Subcat->find('all',array(
                                                                                
'conditions'=>array('Subcat.cat_id' => $catid),
                                                                                
'fields' =>array('Subcat.id','Subcat.subcatname')));
        
$data_combine=Set::combine($data,'{n}.Subcat.id','{n}.Subcat.subcatname');
                                                //$newarr = 
array('lfdl','djfld','lfdjljfd');
                                                
$this->set('options',$data_combine);
                        echo ('<script language="javascript" >alert("ok i dont 
know the
error"); </script>');


                                }

                                                if 
($this->Book->save($this->data)) {
                                        $this->Session->setFlash('The Book has 
been
saved');
                                                        
$this->redirect(array('action'=>'index'), null, true);
                                                } else {
                                                        
$this->Session->setFlash('Book is not saved. Try again.');
                                                }

                                }
                                $data=$this->Author->find('all');
                                $data_combine =
Set::combine($data,'{n}.Author.id','{n}.Author.name');
                                $this->set('selectdata',$data_combine);

                                $data=$this->Cat->find('all');
                                
$data_combine=Set::combine($data,'{n}.Cat.id','{n}.Cat.catname');
                                $this->set('cat',$data_combine);




        }


}//end of class
?>

and my ctp file for the view is

<?php

                echo $javascript->link('prototype');
        echo $form->create('Book');
                echo  $form->input('Book.isbn');
                echo  $form->input('Book.title');
                echo $form->input('Book.description');

                echo $form-
>input('Author.id',array('options'=>array($selectdata),'selected'=>1));
                echo $form-
>input('Cat.id',array('type'=>'select','id'=>'CatID','options'=>array($cat),'selected'=>1,'label'=>'Category'));
                echo $form-
>input('Subcat.id',array('type'=>'select','id'=>'SubcatID','options'=>array(),'label'=>'Subcategory'));


                $options=array('url'=>'add','update'=>'SubcatID');
                echo $ajax->observeField('CatID',$options);

                echo  $form->end('save');
?>

My javascript function  in add() in controller is working showing
that, ajax is able to call the add() when select box for category
display is changed. but the 3rd select box with Id=SubcatID is empty.
As I saw in the urls mentioned above, I used ajax helper and its
method observeField method. But I don't know how to populate items in
the subcategory select box based on the category select box. Kindly
help me.

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