2

i am trying to do a simple form that adds a new comment using ajax to a blog post (actually this is a part of a cakePHP tutorial)

but the problem is that the submit button do nothing at all

here is the part of code that generates the form in the view.ctp file

<?php echo $ajax->form('/comments/add', 'post', array('url' => '/comments/add', 'update' => 'PostsComments', 'indicator' => 'commentSaved'));?>
<fieldset>
    <legend><?php __('Add Comment');?></legend>
        <?php  
            echo $form->hidden('Comment.post_id', array('value' => $post['Post']['id']));
            echo $form->input('Comment.name');
            echo $form->input('Comment.content');                            
        ?>
</fieldset>
<?php echo $form->end('Submit');?>

and here is the add comment action in the comments_controller.php

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');
    }
}      

the problem is that the action add isnt called from the view ... when i viewed the generated html source code i found something like that

<form id="form304217296" onsubmit="event.returnValue = false; return false;" method="post" action="/php-cake/blog/comments/add"><fieldset style="display:none;">

if i removed the on submit tag manually then the action is called but the add_success.ctp is generated as a new page not as an ajax call.

so what could be the problem ?

2 Answers 2

3

finally i have discovered the problem

there was more than one mistake in the tutorial (chapter 8 in CakePHP from novice to professional) after fixing those errors i found another problem is that cakephp v1.2.6 is not compatible with the latest version of prototype (v1.6.1) as it gives the following error in firebug

Event.observe is not a function

so i used version 1.6.0.3 of prototype and the problem was solved.

for a list of mistakes in this chapter see this

Sign up to request clarification or add additional context in comments.

Comments

0

Do you include prototype and scriptaculus in your View/Layout (both are needed for the Ajax-helper)? Also there might be an interference with jquery if you use that too

3 Comments

yes iam using the prototype library but i had put it in app/webroot/js not in the view/layout folder also i didnt use scriptaculus as i think it isnot needed for this simple form ? right ?
I am not sure about scriptaculus (but would think that you don't need that for any ajax-call). What i meant by include in your view/layout: do you have a $javascript->link('prototype'); anywhere in your view or layout ctp, so that in the source code there is a <script type="text/javascript" src="../prototype.js">?
yes i have put the line $javascript->link('prototype'); in the default .ctp found in the layout folder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.