Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6173613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:36:22+00:00 2026-05-23T23:36:22+00:00

I’m trying to use Ajax in CakePHP, and not really getting anywhere! I have

  • 0

I’m trying to use Ajax in CakePHP, and not really getting anywhere!

I have a page with a series of buttons – clicking one of these should show specific content on the current page. It’s important that the page doesn’t reload, because it’ll be displaying a movie, and I don’t want the movie to reset.

There are a few different buttons with different content for each; this content is potentially quite large, so I don’t want to have to load it in until it’s needed.

Normally I would do this via jQuery, but I can’t get it to work in CakePHP.

So far I have:

In the view, the button control is like this:

$this->Html->link($this->Html->image('FilmViewer/notes_link.png', array('alt' =>  __('LinkNotes', true), 'onclick' => 'showNotebook("filmNotebook");')), array(), array('escape' => false));

Below this there is a div called “filmNotebook” which is where I’d like the new content to show.

In my functions.js file (in webroot/scripts) I have this function:

function showNotebook(divId) {
 // Find div to load content to
 var bookDiv = document.getElementById(divId);
 if(!bookDiv) return false;
 $.ajax({ 
   url: "ajax/getgrammar",
   type: "POST",
   success: function(data) {
    bookDiv.innerHTML = data;
   }
  });
 return true;
}

In order to generate plain content which would get shown in the div, I set the following in routes.php:

 Router::connect('/ajax/getgrammar', array('controller' => 'films', 'action' => 'getgrammar'));

In films_controller.php, the function getgrammar is:

  function getgrammar() {
   $this->layout = 'ajax';
   $this->render('ajax');
  }

The layout file just has:

and currently the view ajax.ctp is just:

<div id="grammarBook">
 Here's the result
</div>

The problem is that when I click the button, I get the default layout (so it’s like a page appears within my page), with the films index page in it. It’s as if it’s not finding the correct action in films_controller.php

I’ve done everything suggested in the CakePHP manual (http://book.cakephp.org/view/1594/Using-a-specific-Javascript-engine).

What am I doing wrong? I’m open to suggestions of better ways to do this, but I’d also like to know how the Ajax should work, for future reference.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:36:23+00:00Added an answer on May 23, 2026 at 11:36 pm

    everything you show seems fine. Double check that the ajax layout is there, because if it’s not there, the default layout will be used. Use firebug and log function in cake to check if things go as you plan.

    A few more suggestions: why do you need to POST to ‘ajax/getgrammar’ then redirect it to ‘films/getgrammar’? And then render ajax.ctp view? It seems redundant to me. You can make the ajax call to ‘films/getgrammar’, and you don’t need the Router rule. You can change ajax.ctp to getgrammar.ctp, and you won’t need $this->render('ajax');

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.