CREATE TABLE `comments` ( `id` varchar(36) COLLATE utf8_unicode_ci NOT NULL, `post_id` varchar(36) COLLATE utf8_unicode_ci NOT NULL, `title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `body` text COLLATE utf8_unicode_ci, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; CREATE TABLE `posts` ( `id` varchar(36) COLLATE utf8_unicode_ci NOT NULL, `title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `body` text COLLATE utf8_unicode_ci, `created` datetime DEFAULT NULL, `modified` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
echo $this->Html->script('jquery-1.8.3.min');
public $helpers = array('Js' => array('Jquery');
public $helpers = array('Html','Form','Session','Js' => array('Jquery'),'Paginator');
<div id="comments"> <!-- , AJAX--> <?php foreach($comments as $comment): ?> <div class="comment"> <!— --> <p><b><?php echo $comment['Comment']['title'];?></b></p> <p><?php echo $comment['Comment']['body'];?></p> </div> <?php endforeach;?> <!-- --> <?php echo $this->Form->create('Comment',array('controller' =>'comments', action'=>'add','onSubmit'=>'return false;')); echo $this->Form->input('Comment.title'); echo $this->Form->input('Comment.body'); echo $this->Form->input('Comment.post_id',array('type'=>'hidden','value'=>$comment['Comment']['post_id'])); echo $this->Js->submit('Add Comment',array('url'=>'/comments/add_ajax','update'=>'#comments','evalScripts'=>'true')); echo $this->Js->writeBuffer(); // echo $this->Form->end(); ?> </div>
'onSubmit'=>'return false;' // , echo $this->Js->submit('Add Comment',array('url'=>'/comments/add_ajax','update'=>'#comments','evalScripts'=>'true')); // AJAX echo $this->Js->writeBuffer(); //CakePHP JavaScript
'url'=>'/comments/add_ajax' // , submit. 'update'=>'#comments', // html , (#comments). 'evalScripts'=>'true' // prototype, AJAX .
<div class="submit"> <input id="submit-1030630577" type="submit" value="Add Comment"> </div> <script type="text/javascript"> //<![CDATA[ $(document).ready(function () {$("#submit-1030630577").bind("click", function (event) {$.ajax({data:$("#submit-1030630577").closest("form").serialize(), dataType:"html", evalScripts:"true", success:function (data, textStatus) {$("#comments").html(data);}, type:"post", url:"\/comments\/add_ajax"}); return false;});}); //]]> </script>
//public $helpers = array('Html','Form','Session','Js' => array('Jquery'),'Paginator'); public $helpers = array('Html','Form','Session','Js' => array('Prototype'),'Paginator');
<div class="submit"> <input id="submit-1936204617" type="submit" value="Add Comment"> </div> <script type="text/javascript"> //<![CDATA[ document.observe("dom:loaded", function (event) {$("submit-1936204617").observe("click", function (event) {event.stop(); var jsRequest = new Ajax.Updater("comments", "/comments/add_ajax", {evalScripts:"true", method:"post", parameters:$($("submit-1936204617").form).serialize()});});}); //]]> </script>
//echo $this->Html->script('jquery-1.8.3.min'); echo $this->Html->script('prototype');
public function add_ajax() { if ($this->request->is('post')) { $this->Comment->create(); if ($this->Comment->save($this->request->data)) { $comments=$this->Comment->find('all',array('conditions'=>array('post_id'=>$this->request->data['Comment']['post_id']),'recursive'=>-1)); $this->set(compact('comments')); $this->render('add_success','ajax'); } else { $this->render('add_failure','ajax'); } } }
Source: https://habr.com/ru/post/166653/
All Articles