我正在使用CakePHP 2.2。我正在修改一個動態更新selectbox的方法:http://www.willis-owen.co.uk/2011/11/dynamic-select-box-with-cakephp-2-0/#comment-10773,它的工作沒有問題。當用戶從另一個選擇框中選擇「區域」時,它更新'酒店'選擇框內容。CakePHP更新selectbox中的多個表單字段更改
在同一表格中,當用戶從選擇框中選擇「酒店」時,我希望自動使用「酒店」模型中的地址詳細信息填充多個「團隊」字段。
用戶可以在用戶點擊「團隊」添加視圖上的提交之前修改地址......所有這些。
在組\ add.ctp觀點,我有以下代碼:
echo "<div id='address'>";
echo $this->Form->input('address_1');
echo $this->Form->input('address_2');
echo $this->Form->input('address_3');
echo $this->Form->input('city');
echo $this->Form->input('postcode');
echo $this->Form->input('country');
echo "</div>";
...
$this->Js->get('#TeamHotelId')->event('change',
$this->Js->request(array(
'controller'=>'hotels',
'action'=>'getAddress'
), array(
'update'=> '#address',
'async' => true,
'method' => 'post',
'dataExpression' => true,
'data'=> $this->Js->serializeForm(array(
'isForm' => true,
'inline' => true))
)
)
);
在我HotelsController.php我:
public function getAddress() {
$hotel_id = $this->request->data['Team']['hotel_id'];
CakeLog::write('debug', print_r($hotel_id, true));
$address = $this->Hotel->find('first', array(
'recursive' => -1,
'fields' => array('hotel.address_1', 'hotel.address_2', 'hotel.address_3', 'hotel.city', 'hotel.postcode', 'hotel.country'),
'conditions' => array('Hotel.id' => $hotel_id)
));
CakeLog::write('debug', print_r($address, true));
$this->set('hotels', $address);
$this->set(compact('address'));
$this->layout = 'ajax';
}
酒店\ get_address.ctp:
<?php
echo $this->Form->input('Team.address_1', array('value'=> $address['Hotel']['address_1']));
echo $this->Form->input('Team.address_2', array('value'=> $address['Hotel']['address_2']));
echo $this->Form->input('Team.address_3', array('value'=> $address['Hotel']['address_3']));
echo $this->Form->input('Team.city', array('value'=> $address['Hotel']['city']));
echo $this->Form->input('Team.postcode', array('value'=> $address['Hotel']['postcode']));
echo $this->Form->input('Team.country', array('value'=> $address['Hotel']['country'])); ?>
現在可以工作,代碼已經更新。
我越來越近了。我改變了我的getAddress(),只返回我需要的6個字段。在我的add.ctp中,我把
放在6個字段的周圍。我更改了'update'=>'#address'。現在,當我選擇一家酒店時,6個領域消失了。 – mpe好的。這意味着阿賈克斯正在工作,你就在附近。請確保您的getAddress()操作必須輸出其使用的結果。還要確保你在firebug(在Firefox中)控制檯中的ajax返回。 –
IN螢火蟲我看到
getAddress()不得輸出正確的內容格式。 – mpe