I am new to Cake PHP and I had a website with regular PHP and HTML, but now I am switinching to use Cake PHP. The issue I am having is that it is not picking up the javascript for a slider I have on a menu. (But it works on the original web).
So I did the following steps, maybe you can see where I am making my mistake.
First I put the Javascript file under webroot/js
FILE javascript.js
/**
*
*/
$(document).ready(function() {
//ACCORDION BUTTON ACTION
$('div.accordionButton').click(function() {
$('div.accordionContent').slideUp('normal');
$(this).next().slideDown('normal');
});
//HIDE THE DIVS ON PAGE LOAD
$("div.accordionContent").hide();
});
Then I went to AppController and added the following line under cake/libs/controller/app_Controller
class AppController extends Controller {
var $helpers = array('Html', 'Form', 'Javascript');
}
Then, that change allowd me to add it to layout
FILE mylayout.ctp
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<?php
$javascript->link('javascript.js');
</head>
<?php include 'menu_nav.php';?> //this has starting <body> tag
<?php include 'leftmenu_nav.php';?>
<div id="content" class="divContent">
<div id="mainContent" class="divMainContent" style="text-align: left;">
<?php echo $content_for_layout; ?>
</div>
</div>
<?php include 'footer.php';?> //this had end </body> tag
</html>
Now somewhere in the
$content_for_layout
there some <divs> that make use of the JS, but for some reason 1) I do not see the JS markup when i do page source and 2) It is not sliding like it used to..
Can anyone please tell me what I may be missing in this CakePHP config?.
Thank you
echo $this->Html->script('javascript.js');
echo $this->Html->script('jquery-ui-1.8.16.custom.min.js');
echo $this->Html->script('jquery.js');
echo $this->Html->script('jquery.MetaData.js');
echo $this->Html->script('jquery.min.js');
echo $this->Html->script('jquery.rating.js');
echo $this->Html->script('jquery.rating.pack.js');
echo $this->Html->script('jquery.validate.js');
echo $this->Html->script('paging.js');
echo $this->Html->script('ui.datetimepicker.js');
echo $this->Html->script('format.js');
There are a couple of things you need to check.
First, don’t change anything in the cake folder. That is a big no no. If you want to have an app controller, simply create a new file name app_controller.php and put it in your /app folder. Add the following code in it.
How you include the JS file depends on what version of cake your are using. The newer version uses the following syntax
http://book.cakephp.org/view/1589/script (1.3)
Next, make sure that all the JS is actually being included. Look at the source and click through to the JS file to confirm that the file is loaded. I don’t see a jquery include anywhere so you should confirm that jquery is included in your layout.
After that, confirm that the
div.accordionButtondiv is contained in your view. Since it doesn’t look like it’s in the layout, open up the specific view element and check in there.As a last resort, add
alert($('div.accordionButton').length)in your JS file to confirm that the JS is actually picking up the DIV from the view. If the alert shows 0, then this means that it’s not.