Kek PHP Ajax Yardımcısı ile sorun

4 Cevap php

I am trying to implement the Ajax feature in the comments section of my blog. I have downloaded prototype-1.6.0.3.js and have placed it in the js folder inside webroot. I have made the following changes in the layout file(default.ctp)

$javascript->link(array('prototype'));

Aynı zamanda, aşağıdaki kod, kontrol eklendi

var $helpers = array('Html', 'Form', 'Ajax','Javascript');

Bu posts_controller.php dosyasında benim kodu

function view($id = null) {
    	if (!$id) {
    		$this->Session->setFlash(__('Invalid Post.', true));
    		$this->redirect(array('action'=>'index'));
    	}
    	$post = $this->Post->read(null,$id);
    	$comments = $this->Post->Comment->find('all',
array('conditions'=>array('Post.id'=>$id)));
    	$this->set(compact('post','comments'));
    }

View.ctp dosyasında Benim kod

<h2>Comments</h2>
<div id="comments">
<?php foreach($comments as $comment): ?>
  <div class="comment">
<p><b><?php echo $comment['Comment']['name']; ?></b></p>
      <p><?php echo $comment['Comment']['content']; ?></p>
</div>
<?php endforeach; ?>
<?php echo $ajax->form('/comments/add','post',array('update'=>'comments'));?>
   <?php echo $form->input('Comment.name');?>
   <?php echo $form->input('Comment.content');?>
   <?php echo $form->input('Comment.post_id',array('type'=>'hidden','value'=>$post['Post']['id']));?>
   <?php echo $form->end('Add Comment');?>
</div>

Ben comments_controller.php aşağıdaki fonksiyonu da ekledik

function add() {
    	if (!empty($this->data)) {
    		$this->Comment->create();
    		if ($this->Comment->save($this->data)) {
    			$comments = $this->Comment->find('all',array('conditions'=>array('post_id'=>$this->data['Comment']['post_id']),'recursive'=>-1);
    				$this->set(compact('comments'));
    				$this->render('add_success','ajax');
    				} else {
    				$this->render('add_failure','ajax');
    				}
    			 }
    		 }

Ve sonra add_success.ctp dosyasındaki kodudur

<?php foreach($comments as $comment): ?>
<div class="comment">
<p><b><?php echo $comment['Comment']['name'];?></b></p>
<p><?php echo $comment['Comment']['content'];?></p>
</div>
<?php endforeach;?>

Şimdi sorun ben yorum eklemek mümkün değilim olmasıdır. Ben add comments butonuna tıkladığımda hiçbir şey olmuyor. Ben elle databse yorum eklemiş ve çalışıyor. Ben Ajax Yardımcısı çalışıyorum ama ne zaman sorunlar yaşıyorum.

? Nerede sorun Ve adamlar, böyle uzun bir soru için üzgünüm?. [: (]

4 Cevap

Kundakçı yüzden bunu ajax istekte olup olmadığını görmek için test edebilirsiniz yüklü var mı?

, Belki de yaptığını $ form-> ('Yorum Ekle') submit deneyin düğme oluşturmak ve bir fark yapar ve aslında ajax isteği gerçekleştirir olmadığını görmek için.

Eğer herhangi bir javascript hataları alır mıyım?

sizden örnek takip ediyorsun gibi "Beginning CakePHP" ben de aynı hatayı yaptı düşünüyorum görünüyor

Bu kitapta söz değil, ama js dosyasının adı (webroota / js) yardımcı fonksiyon argüman maç, bu nedenle one bu yapmanız gerekir.:

1) prototype.js için webroota / js klasörü içindeki prototip-1.6.0.3.js () yeniden adlandırmak

VEYA

2) yardımcı işlevini değiştirmek

$javascript->link(array('prototype-1.6.0.3'));

Doğru hatırlıyorum, ayrıca doğru da çalışmaya ajax fonksiyonları inşa için scriptaculous dahil etmek gerekir ...

Bu gerçekten bir cevap değil ama ben de bu sorun koştu o söz onun değer düşündüm. Ve Evet @ smchacko bahsettiği o kitabı kullanıyorum. Ben kullanarak Prototype Version 1.7 am:

//this is how I called it and renamed it to prototype
<?php echo $javascript->link( array('prototype')); ?>

İlginçtir ki söz ben ateş bug yanı sıra web Developer tool bar var. Kundakçı hataları dönmek yok ama Chris Pederic plug-in söz onun çok çeşitli uyarılar ... vardır. Bu, ekranın sağ üst köşesinde görüntülenir