0
我想在CakePHP中做一個自動完成的函數,但沒有成功。我試了下面的代碼。如何在cakephp中自動完成表單?
public function find() {
if ($this->request->is('ajax')) {
$this->autoRender = false;
$country_name = $this->request->data['Country']['name'];
$results = $this->Country->find('all', array(
'conditions' => array('Country.name LIKE ' => '%' . $country_name . '%'),
'recursive' => -1
));
foreach($results as $result) {
echo $result['Country']['name'] . "\n";
}
echo json_encode($results);
}
}
//形式和jQuery
<?php
echo $this->Form->create('Country', array('action' => 'find'));
echo $this->Form->input('name',array('id' => 'Autocomplete'));
echo $this->Form->submit();
echo $this->Form->end();
?>
<script type="text/javascript">
$(document).ready(function($){
$('#Autocomplete').autocomplete({
source:'/countries/find',
minLength:2
});
});
</script>
我試過你的代碼,但它沒有任何效果。 – 2014-09-19 12:31:00
我在控制器代碼中添加了$ country_name = $ this-> request-> query('term'),現在它正在工作。感謝您的幫助。 – 2014-09-20 03:06:53