cakephp 1.3 ajax autocomplete not working. autocomplete=off
Can not get my ajax to work at all. below is my code. I am trying to use autocomplete
Post Controller
<?php
class PostsController extends AppController {
var $name = 'Posts';
var $helpers = array('Html','Ajax','Javascript');
//Extra Functionality
function autoComplete() {
//Partial strings will come from the autocomplete field as
//$this->data['Post']['subject']
$this->set('topics', $this->Post->Topic->find('all', array(
'conditions' => array(
'Topic.name LIKE' => '%'.$this->data['Topic']['Topic'].'%'
),
'fields' => array('Topic')
)));
$this->layout = 'ajax';
}
}
FORM add.ctp
<div class="posts form">
<?php echo $this->Form->create('Post');?>
<fieldset>
<legend><?php __('Add Post'); ?></legend>
<?php
echo $this->Form->input('comment');
echo $this->Form->input('user_id');
//echo $this->Form->input('Topic',array('type'=>'text'));
echo $ajax->autoComplete('Topic', '/posts/autoComplete');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
</div>
ajax Form that开发者_如何转开发 should dynamically appear
<ul>
<?php foreach($topics as $topic): ?>
<li><?php echo $item['Topic']['name']; ?></li>
<?php endforeach; ?>
</ul>
and the js addon in the default.ctp
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head>
<title>test page</title>
<?php echo $this->Html->css('skin'); ?>
<?php echo $html->script('prototype');echo $html->script('scriptaculous'); ?>
</head>
<body>
<div id="wrapper">
<div class="header">
Gossip
</div>
<div id="content">
<?php echo $content_for_layout ?>
</div>
<div class="footer">
</div>
</div>
</body>
</html>
I also checked google chrome and all of the script files are loaded correctly. And yes I did add all scriptulicious files and prototype.
THANKS :D
Are you sure you're rolling Cake 1.3? So much of what you're rolling is appropriate for Cake 1.2 and deprecated in 1.3 that I feel like I should make sure.
See: http://book.cakephp.org/view/1358/AJAX
Fix your helpers. Javascript and Ajax are deprecated in 1.3. Also, you're missing the RequestHandler component which is needed to detect ajax responses in both 1.2 and 1.3:
var $helpers = array('Html', 'Form', 'Js'=>array("Jquery"));
var $components = array('RequestHandler');
where Jquery can be the name of whatever library you prefer (although Jquery rolls by default.)
Also, in your default.ctp, there should be a line to the effect of <?= $scripts_for_layout ?>
in your layout's header. There should be a line to the effect of <? $this->Js->writeBuffer(); ?>
at the end of your layout markup, just before the closing body tag, to flush buffered scripts.
Note: if you are rolling 1.3, some people feel that $this->Html->script
is better practice than $html->script
. I tend to agree.
Rolling two libs (scriptaculous and prototype? are you sure this is necessary?) comes with it's own set of considerations and techniques to ensure all included libs are running in compatibility mode. For the sake of debugging, consider simplifying this at least to a single lib, if jQuery just won't work for you. Again, jQuery rolls by default. You would need to include the Js helper correctly as demonstrated above except for your lib of choice if you want to override the default with one of the other libs. Also, some response types require you create a layout for them (to avoid header sent errors, for example) such as json, some ajax, etc. so RequestHandler can serve them correctly.
Sort all this out and refer to the Js Helper, Html Helper, and Form Helper to set up your autoComplete() functionality and it should work fine. Or revert to 1.2 and use Javascript and Ajax to your heart's content. :S
HTH :)
精彩评论