我是CakePHP和一般编码的初学者。我正在尝试创建一对动态下拉菜单,其中第二个菜单中的选择取决于第一个菜单中选择的内容。我遵循了许多指南,但似乎没有取得好成绩。最近,我在这里找到了这个问题的答案:CakePHP重新填充列表框,但当使用谷歌chrome时,我得到的只是一个"ReferenceError: $ is not defined"
。
我正在使用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'));
}
}
查看/users/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'));
}
}
查看/citys/get_cities.ctp
<?php
if(!empty($cities)){
foreach ($cities as $id => $name) {
?>
<option value="<?php echo $id; ?>"><?php echo $name; ?></option>
<?php
}
}
?>
您的网站上似乎没有包含jquery库。您可以通过两种方式做到这一点:下载一个文件并放入webroot/js
目录,或者从Google CDN链接它。
如果您选择第一个解决方案,您必须访问jQuery官方页面并下载库的当前版本。然后将文件移动到APP/webroot/js
路径下的项目中。最好将文件名更改为jquery.js
,而不提供版本信息。最后一件事是打开您的布局文件,该文件将位于APP/View/Layouts/your_layout_file.ctp
中。如果您没有自定义布局,它将被称为default.ctp
。最后一步是找到<head>
标签并放置以下行:
<?php echo $this->Html->script('jquery'); ?>
或者,若不更改名称,就必须将文件的整个名称作为无扩展名的param传递给它。
如果你想要来自外部来源的链接库,比如谷歌CDN,你只需要从网站上获得jQuery的url,并在布局文件中放上以下行:
<?php echo $this->Html->script('http://code.jquery.com/jquery.min.js'); ?>
有关HtmlHelper
和script()
方法的更多信息,您可以在CakePHP文档中阅读。