cakephp Ajax: city select box not populating when state is selected -
i'm trying populate city select box based on selected state (which select box) using ajax. when select state, city select box not being populated.
there 5 models below: student, merryparent, merryclass, state, city. of them related 1 another.
can please tell me on doing wrong? thank you.
the following models: student.php
<?php class student extends appmodel{ var $name='student'; var $belongsto=array('merryparent','state','city','merryclass'); } ?>
merry_parent.php
<?php class merryparent extends appmodel{ var $name='merryparent'; $hasmany=array( 'student'=>array( 'classname'=>'student', 'foreignkey'=>'merry_parent_id' ) ); $belongsto=array('state','city','merryclass'); ?>
merry_class.php
<?php class merryclass extends appmodel{ var $name='merryclass'; var $hasmany=array ('student'=>array( 'classname'=>'student', 'foreignkey'=>'class_id' ), 'merryparent' ); var $belongsto=array('state','city'); //var $displayfield='class_name'; } ?>
city.php
<?php class city extends appmodel{ var $name='city'; var $belongsto='state'; var $hasmany=array('merryparent','merryclass', 'student'=>array( 'classname'=>'student', 'foreignkey'=>'city_id' ) ); } ?>
state.php
<?php class state extends appmodel{ var $name='state'; var $hasmany=array( 'merryparent', 'merryclass', 'city'=>array( 'classname'=>'city', 'foreignkey'=>'state_id' //'dependent'=>true ), 'student'=>array( 'classname'=>'student', 'foreignkey'=>'state_id' ) ); } ?>
the controllers students_controller.php
<?php class studentscontroller extends appcontroller{ var $name='students'; var $helpers = array('html','form','ajax','javascript'); var $components=array('requesthandler'); function getcities(){ $options=$this->student->city->find('list', array ('conditions'=>array( 'city.state_id'=>$this->data['student']['state_id'] ), 'group'=>array('city.name') ) );//closing parentheses find('list'... $this->render('/students/ajax_dropdown'); } function add(){ if (!empty($this->data)){ /*var_dump($this->data); die(debug($this->student->validationerrors)); */ $student=$this->student->saveall($this->data,array('validate'=>'first')); if (!empty($student)) { $this->session->setflash('your child\'s admission has been received. send email shortly.'); $this->redirect(array('controller'=>'pages', 'action'=>'home')); } } //for if (!empty.... $states=$this->student->state->find('list'); $cities=array(); $this->set(compact('states','cities')); }//end function
} ?>
merry_parents_controller.php
<?php class merryparentscontroller extends appcontroller{ var $name='merryparents'; } ?>
add.ctp
<?php echo $javascript->link('prototype',false); echo $form->create('student'); echo '<fieldset>'; echo '<legend>student information</legend>'; echo $form->input('student.name'); $options = array('male'=>'male','female'=>'female'); $attributes = array('value'=>'male'); echo $form->radio('student.gender',$options,$attributes); echo $form->input('student.dob', array('label'=>'date of birth', 'dateformat'=>'dmy', 'empty'=>true, 'timeformat' => '', 'minyear' => ( date('y') - 5 ), 'maxyear' => ( date('y') - 2 ) )); echo $form->input('student.class_id', array( 'label'=>'enquiry class for', 'empty'=>'choose one', 'options'=>array('1'=>'playgroup','2'=>'nursery','3'=>'lkg', '4'=>'ukg') ) ); echo '</fieldset>'; echo '<fieldset>'; echo '<legend>parent information</legend>'; //echo $form->input('student.parent_id', array('type'=>'hidden')); echo $form->input('merryparent.initial', array('empty'=>true, 'options'=>array('dr'=>'dr', 'mr'=>'mr', 'mrs'=>'mrs', 'ms'=>'ms') ) ); echo $form->input('merryparent.name', array('label'=>'parent/guardian name')); echo $form->input('merryparent.email'); echo $form->input('merryparent.landline'); echo $form->input('merryparent.mobile'); echo $form->input('merryparent.address'); echo $form->input('student.state_id'); echo $form->input('student.city_id'); echo $form->input('merryparent.postal_code'); $options = array('url' => 'getcities', 'update' => 'studentcityid'); echo $ajax->observefield('studentstateid', $options); //observes drop down changes in state id , makes xmlhttprequest when contents have changed. echo '</fieldset>'; echo $form->end('submit'); ?>
ajax_dropdown.ctp
<?php foreach($options $k=>$v) : ?> <option value="<?php echo $k; ?>"><?php echo $v; ?></option> <?php endforeach; ?>
you don't need put 0 in model name , field, since saving 1 student in form. try using student.name instead of student.0.name , on fields. try test $validate => false if saves then.
Comments
Post a Comment