Wednesday, May 25, 2011

ajax not updating county

I have a three tables called links,state and county.

what im trying to do is when you select the state it will display the
counites for that state.

The list of states poulates but nothing is apperaing in the counties
select drop down
I am getting no erroes and i am clueless on what to do next. I am
using prototype for my javascript


In my links tabel i have these fields
county_id state_id id

my links model it looks like this

class Link extends AppModel {
var $name = 'Link';
var $displayField = 'name';
//The Associations below have been created with all possible keys,
those that are not needed can be removed

var $belongsTo = array('County','State');

}


states table
fields
id abbr name

state model

class State extends AppModel {
var $name = 'State';
var $displayField = 'name';

var $hasMany = array('County','Link');


}

fileds in counties table

name id
county model


class County extends AppModel {
var $name = 'County';
var $displayField = 'name';
//The Associations below have been created with all possible keys,
those that are not needed can be removed


var $BelongsTo = array('State');
var $hasMany = array('Link');
var $order = "County.name ASC";
}


link contorller

<?php
class LinksController extends AppController {

var $name = 'Links';
var $helpers = array('Ajax', 'Javascript');
var $components = array('RequestHandler');

function index() {
$this->Link->recursive = 0;
$this->set('links', $this->paginate());
}


function add() {
if (!empty($this->data)) {
$this->Link->create();
if ($this->Link->save($this->data)) {
$this->Session->setFlash(__('The Link has been saved', true));
$this->redirect(array('action'=>'index'));
} else {
$this->Session->setFlash(__('The Link could not be saved. Please,
try again.', true));
}
}
$states = $this->Link->State->find('list');
$counties = $this->Link->County->find('list');
$this->set(compact('states', 'counties'));
}

function update_region_select() {
if(!empty($this->data['Link']['state_id'])) {
$options = $this->requestAction('/counties/getlist/'.$this-
>data['Link']['state_id']);
$this->set('options',$options);
}
}


}
?>


county controller

class CountiesController extends AppController {

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


function getlist($state_id=null) {
if (!$state_id) {
return $this ->County->find('list');
} else {
return $this->County-
>find('list',array('conditions'=>array('state_id'=>$state_id)));
}
}

link view


<div class="links form">
<?php echo $this->Form->create('link');?>
<fieldset>
<legend><?php __('Add Link');?></legend>
<?php
echo $this->Form-
>select('state_id',array($states),null,array('id'=>'states'),false);
?><br /><br /><?
echo $this->Form-
>select('county_id',array(),null,array('id'=>'counties'),false);

?>
</fieldset>
<?php echo $ajax-
>observeField('states',array('url'=>'update_county_select','update'=>'counties'));?
>

<?php echo $this->Form->end('Submit');?>
</div>


here's my 'update_county_select

<?php
if (!empty($options)) {
foreach ($options as $k => $v) {
echo "<option value='$k'>$v</option>";
}
}
?>

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: