jason
jason

Reputation: 3615

CakePhp and jQuery not working

I'm still really new to CakePhp, so if I leave anything out, let me know. I am trying to create a form that post back to the controller using ajax. Here is what I have:

Model: EventExampleModel

class EventExampleModel extends AppModel {

}

View: /app/View/EventExample/index.ctp

<?php

$data = $this->Js->get('#CommentSaveForm')->serializeForm(array('isForm' => true, 'inline' => true));
$this->Js->get('#CommentSaveForm')->event(
    'submit',
    $this->Js->request(
        array('action' => 'save'),
        array(
            'update' => '#commentStatus',
            'data' => $data,
            'async' => true,
            'dataExpression'=>true,
            'method' => 'POST'
        )
    )
);
echo $this->Form->create('Commenter', array('action' => 'save', 'default' => false));
echo $this->Form->input('Commenter.comments_name');
echo $this->Form->input('Commenter.comments_text');
echo $this->Form->end(__('Submit'));
echo $this->Js->writeBuffer();

?>

<div id="contactStatus">


</div>

Controller: EventExampleController:

class EventExampleController extends Controller {
  public $helpers = array('Html', 'Form');
    public function index() {
        $this->layout = 'homePage';
    }
    public function save() {

        if ($this->request->is('ajax')) {
            $this->render('contact-ajax-response', 'ajax');
        }
    }
}

and my layout:

<?php
$cakeDescription = __d('cake_dev', 'Employee Database');
?>
<!DOCTYPE html>
<html>
<head>

    <title>
        <?php echo $cakeDescription ?>
    </title>
    <?php
    echo $this->Html->meta('icon');
    echo $this->Html->css('styles');
    echo $this->fetch('meta');
    echo $this->fetch('script');
    echo $this->Html->script('jquery-1.10.2.js'); // Include jQuery library
    echo $this->Html->script('myScripts');
    ?>
</head>
<body>
<div class="homepage-container">
    <div id="content">
        <?php echo $this->fetch('content'); ?>
    </div>
    <div id="footer">

    </div>
</div>
</body>
</html>

I also have the jquery library located in the app/webroot/js folder.

The problem is that when the page renders and I click the submit button, nothing happens. I see no errors on the page or in the console and the page does not post back. What am I missing? I expect the page to post back to the server, asynchronously and my save function should get called (the break point never gets hit).

Can anyone help out?

Upvotes: 0

Views: 662

Answers (1)

Fernando Coronato
Fernando Coronato

Reputation: 56

Your model name is wrong

The right convention is

EventExample extends AppModel

And file name EventExample.php

Just a tip.

Open your firebug or equivalent in google chrome and check the network tab. Check if something is triggered when you click the button please.

Upvotes: 1

Related Questions