2014-10-07 26 views
0

我是CakePHP和一般编码的初学者。我正在尝试创建一对动态下拉菜单,其中第二个选项取决于第一个选择的内容。我遵循了一些指南,但我似乎没有得到好的结果。最近我跟着这里的问题的答案:CakePHP re-populate list box但我得到的是一个"ReferenceError: $ is not defined"当使用谷歌浏览器。CakePHP中的动态下拉菜单在Chrome中引发[ReferenceError]

我使用CakePHP 2.5.4与实例中所给出

我的用户控制数据库:

<?php 

function beforeFilter()//executed before any controller action logic 
{ 
      $this->Security->enabled = false; 


    } 
App::uses('AppController', 'Controller'); 
/** 
* Users Controller 
* 
* @property User $User 
* @property PaginatorComponent $Paginator 
* @property SessionComponent $Session 
*/ 
class UsersController extends AppController { 

/** 
* Components 
* 
* @var array 
*/ 
    public $components = array('Paginator', 'Session'); 

/** 
* index method 
* 
* @return void 
*/ 
    public function index() { 
     $this->User->recursive = 0; 
     $this->set('users', $this->Paginator->paginate()); 
    } 

/** 
* view method 
* 
* @throws NotFoundException 
* @param string $id 
* @return void 
*/ 
    public function view($id = null) { 
     if (!$this->User->exists($id)) { 
      throw new NotFoundException(__('Invalid user')); 
     } 
     $options = array('conditions' => array('User.' . $this->User->primaryKey => $id)); 
     $this->set('user', $this->User->find('first', $options)); 
    } 

/** 
* add method 
* 
* @return void 
*/ 
    public function add() { 
     if ($this->request->is('post')) { 
      $this->User->create(); 
      if ($this->User->save($this->request->data)) { 
       $this->Session->setFlash(__('The user has been saved.')); 
       return $this->redirect(array('action' => 'index')); 
      } else { 
       $this->Session->setFlash(__('The user could not be saved. Please, try again.')); 
      } 
     } 
     $countries = $this->User->Country->find('list'); 
     $cities = $this->User->City->find('list'); 
     $this->set(compact('countries', 'cities')); 
     // populate selects with options 
    $this->set('countries', $this->User->Country->find('list')); 
    $this->set('cities', $this->User->City->find('list')); 

    } 

/** 
* edit method 
* 
* @throws NotFoundException 
* @param string $id 
* @return void 
*/ 
    public function edit($id = null) { 
     if (!$this->User->exists($id)) { 
      throw new NotFoundException(__('Invalid user')); 
     } 
     if ($this->request->is(array('post', 'put'))) { 
      if ($this->User->save($this->request->data)) { 
       $this->Session->setFlash(__('The user has been saved.')); 
       return $this->redirect(array('action' => 'index')); 
      } else { 
       $this->Session->setFlash(__('The user could not be saved. Please, try again.')); 
      } 
     } else { 
      $options = array('conditions' => array('User.' . $this->User->primaryKey => $id)); 
      $this->request->data = $this->User->find('first', $options); 
     } 
     $countries = $this->User->Country->find('list'); 
     $cities = $this->User->City->find('list'); 
     $this->set(compact('countries', 'cities')); 
    } 

/** 
* delete method 
* 
* @throws NotFoundException 
* @param string $id 
* @return void 
*/ 
    public function delete($id = null) { 
     $this->User->id = $id; 
     if (!$this->User->exists()) { 
      throw new NotFoundException(__('Invalid user')); 
     } 
     $this->request->allowMethod('post', 'delete'); 
     if ($this->User->delete()) { 
      $this->Session->setFlash(__('The user has been deleted.')); 
     } else { 
      $this->Session->setFlash(__('The user could not be deleted. Please, try again.')); 
     } 
     return $this->redirect(array('action' => 'index')); 
    } 
} 

查看/用户/ add.ctp

<?php 
echo $this->Form->create('User'); 
echo $this->Form->input('country_id'); 
echo $this->Form->input('city_id'); 
echo $this->Form->input('name'); 
echo $this->Form->end('Submit'); 

$this->Js->get('#UserCountryId')->event('change', 
    $this->Js->request(
     array('controller' => 'countries', 'action' => 'get_cities'), 
      array(
       'update' => '#UserCityId', 
       'async' => true, 
       'method' => 'post', 
       'type' => 'json', 
       'dataExpression' => true, 
       'evalScripts' => true, 
       'data' => $this->Js->serializeForm(array('isForm' => true, 'inline' => true)), 
     ) 
    ) 
); 

国家控制器

<?php 
App::uses('AppController', 'Controller'); 
/** 
* Countries Controller 
* 
* @property Country $Country 
* @property PaginatorComponent $Paginator 
* @property SessionComponent $Session 
*/ 
class CountriesController extends AppController { 

/** 
* Components 
* 
* @var array 
*/ 
    public $components = array('Paginator', 'Session'); 

public function get_cities(){ 
    Configure::write('debug', 1); 
    $cities = array(); 
    if(isset($this->request->query['data']['User']['country_id'])){ 
     $cities = $this->Country->City->find('list', array(
        'conditions' => array('City.country_id' => $this->request->query['data']['User']['country_id']) 
     )); 
    } 
    $this->set('cities', $cities); 
} 
/** 
* index method 
* 
* @return void 
*/ 
    public function index() { 
     $this->Country->recursive = 0; 
     $this->set('countries', $this->Paginator->paginate()); 
    } 

/** 
* view method 
* 
* @throws NotFoundException 
* @param string $id 
* @return void 
*/ 
    public function view($id = null) { 
     if (!$this->Country->exists($id)) { 
      throw new NotFoundException(__('Invalid country')); 
     } 
     $options = array('conditions' => array('Country.' . $this->Country->primaryKey => $id)); 
     $this->set('country', $this->Country->find('first', $options)); 
    } 

/** 
* add method 
* 
* @return void 
*/ 
    public function add() { 
     if ($this->request->is('post')) { 
      $this->Country->create(); 
      if ($this->Country->save($this->request->data)) { 
       $this->Session->setFlash(__('The country has been saved.')); 
       return $this->redirect(array('action' => 'index')); 
      } else { 
       $this->Session->setFlash(__('The country could not be saved. Please, try again.')); 
      } 
     } 
    } 

/** 
* edit method 
* 
* @throws NotFoundException 
* @param string $id 
* @return void 
*/ 
    public function edit($id = null) { 
     if (!$this->Country->exists($id)) { 
      throw new NotFoundException(__('Invalid country')); 
     } 
     if ($this->request->is(array('post', 'put'))) { 
      if ($this->Country->save($this->request->data)) { 
       $this->Session->setFlash(__('The country has been saved.')); 
       return $this->redirect(array('action' => 'index')); 
      } else { 
       $this->Session->setFlash(__('The country could not be saved. Please, try again.')); 
      } 
     } else { 
      $options = array('conditions' => array('Country.' . $this->Country->primaryKey => $id)); 
      $this->request->data = $this->Country->find('first', $options); 
     } 
    } 

/** 
* delete method 
* 
* @throws NotFoundException 
* @param string $id 
* @return void 
*/ 
    public function delete($id = null) { 
     $this->Country->id = $id; 
     if (!$this->Country->exists()) { 
      throw new NotFoundException(__('Invalid country')); 
     } 
     $this->request->allowMethod('post', 'delete'); 
     if ($this->Country->delete()) { 
      $this->Session->setFlash(__('The country has been deleted.')); 
     } else { 
      $this->Session->setFlash(__('The country could not be deleted. Please, try again.')); 
     } 
     return $this->redirect(array('action' => 'index')); 
    } 
} 

查看/城市/ get_cities.ctp

<?php 
    if(!empty($cities)){ 
     foreach ($cities as $id => $name) { 
?> 
<option value="<?php echo $id; ?>"><?php echo $name; ?></option> 
<?php   
     } 
    } 
?> 
+1

你确定你添加了jQuery库例如在你的布局? – marian0 2014-10-07 20:27:58

+0

@ marian0对不起,我是一个完整的noob,你能为我详细说明吗? :) – Farstucker 2014-10-10 20:58:50

回答

0

看来,你不包括jQuery库在您的网站上。您可以通过以下两种方式执行此操作:下载文件并将其放入webroot/js目录或从Google CDN链接它。

如果您选择第一个解决方案,您必须访问jQuery official page并下载当前版本的库。然后将文件移动到APP/webroot/js路径下的项目。没有关于版本的信息,将文件的名称更改为jquery.js是个好习惯。最后一件事是打开你的布局文件sholud位于APP/View/Layouts/your_layout_file.ctp。如果您没有自定义布局,它将被称为default.ctp。最后一步是寻找<head>标签,并把下面一行:

<?php echo $this->Html->script('jquery'); ?> 

或者,如果你不改变名称,你必须通过有整个文件的特性参数的名称,没有扩展名。

如果你想从谷歌CDN你只需要从网站获得的URL的jQuery,并把下面一行在你的布局文件外部源链接库:

<?php echo $this->Html->script('http://code.jquery.com/jquery.min.js'); ?> 

更多HtmlHelperscript()方法,你可以阅读CakePHP documentation