我想在 ContactUs 表单上实现 Ajax,这是我的代码。控制器:
class ContactsController extends AppController {
public $layout = 'default';
public $helpers = array('Html', 'Form', 'Paginator','Js');
public $components = array('RequestHandler');
public function index(){
if(!empty($this->data)){
if($this->Contact->save($this->data)){
if($this->RequestHandler->isAjax()){
$this->render('success','ajax');
} else {
$this->Session->setFlash('Message sent');
$this->redirect(array('action'=>'index'));
}
}
}
}
}
查看文件
<?php echo $this->Html->script('jquery', FALSE); ?>
<div id="success"></div>
<h2>Contact Us</h2>
<?php
echo $this->Form->create();
echo $this->Form->input('name',array('id'=>'name'));
echo $this->Form->input('email',array('id'=>'email'));
echo $this->Form->input('message',array('id'=>'message'));
echo $this->Js->submit('Send',array(
'before'=>$this->Js->get('#sending')->effect('fadeIn'),
'success'=>$this->Js->get('#sending')->effect('fadeout'),
'update'=>'#success'
));
echo $this->Form->end();
?>
<div id="sending" style=" display: none;background-color: #90ee90;">Sending...</div>
在 view/layouts/ajax.ctp 文件中包含:
<?php //echo $this->fetch('content'); ?>
<?php echo $content_for_layout; ?>
我已经尝试过这两种方法。并且 default.ctp 布局包含在 head 部分:
echo $scripts_for_layout;
echo $this->Js->writeBuffer(array('cache'=>TRUE));
在源文件中包含 jquery,但 PHPStorm 在 jquery 文件中显示错误,并且 firebug 显示
希望一切都会清楚,非常感谢任何帮助。