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は表示します
すべてが明確になることを願っています。