-1

I have created custom module in admin with the name createadmincontroller module and it configure the config.xml.i want to call the controller index function from .phtml with ajax but it throw an error "Uncaught TypeError: undefined is not a function" can any one tell me where I went wrong? below is my full module details:

my code is:

JR->CreateAdminController->controllers->Adminhtml->CustomController.php

<?php
class JR_CreateAdminController_Adminhtml_CustomController extends Mage_Adminhtml_Controller_Action
{
public function indexAction()
{
if(isset($_POST['data'])){
echo 'successful';
//exit;
}
    $this->loadLayout()
        ->_setActiveMenu('mycustomtab')
        ->_title($this->__('Index Action'));



    $this->renderLayout();
}
?>

JR->CreateAdminController->etc->config.xml

<?xml version="1.0"?>
<config>
<modules>
    <JR_CreateAdminController>
        <version>1.0.0</version>
    </JR_CreateAdminController>
</modules>
<global>
    <helpers>
        <jr_createadmincontroller>
            <!-- Helper definition needed by Magento -->
            <class>Mage_Core_Helper</class>
        </jr_createadmincontroller>
    </helpers>
</global>
<admin>
    <routers>
        <adminhtml>
            <args>
                <modules>
                    <jr_createadmincontroller before="Mage_Adminhtml">JR_CreateAdminController_Adminhtml</jr_createadmincontroller>
                </modules>
            </args>
        </adminhtml>
    </routers>
</admin>
<adminhtml>
<layout>
    <updates>
  <petra>
      <file>createadmincontroller.xml</file>
     </petra>
 </updates>   
</layout>
</adminhtml>

app->design->adminhtml->default->default->layout->createadmincontroller.xml

<?xml version="1.0"?>
<layout version="0.1.0">
<adminhtml_custom_index>
    <reference name="content">
        <block type="adminhtml/template" name="createadmincontroller" template="createadmincontroller/index.phtml" />
    </reference>
</adminhtml_custom_index>
</layout>

app->design->adminhtml->default->default->template->createadmincontroller->index.phtml

<button type="button" class="scalable" onclick="test()">Click Me!</button>
<script>
function test(){
alert("wao");   
var t = '<?php echo Mage::getUrl('*/custom');   ?>';
$.ajax({
        url: "<?php echo $this->getUrl('*/custom/'); ?>"

        }).done(function() {
            alert("Hey");
        });
}
</script>

JR->CreateAdminController->Helper->Data.php

<?php
class JR_CreateAdminController_Helper_Data extends Mage_Core_Helper_Abstract
{} 
?>
7ochem
  • 2,183
  • 1
  • 34
  • 42
Priyank
  • 3,778
  • 3
  • 29
  • 48
  • two things to correct yet : bad habit you should really loose is to close php on a class. You should never ever do this. please read http://stackoverflow.com/questions/4410704/why-would-one-omit-the-close-tag Another thing is using $_POST. Magento has built in functionalities for that, inherited from ZF. Instead please use `$this->getRequest()->getPost('data');` – β.εηοιτ.βε Jan 30 '15 at 08:57
  • how i call index function from controller by ajax.? – Priyank Jan 30 '15 at 09:41

1 Answers1

3

Did you try tu use jQuery.ajax() instead of $.ajax()?

Magento uses Prototype JS, which has already claimed the $ global variable. So you need to use jQuery in no-conflict mode and use jQuery instead.

Alternatives

  1. Use $j.ajax() after applying no-conflict mode with var $j = jQuery.noConflict(); (thanks @b.enoit.be)
  2. Wrap your jQuery code inside a closure: (function($){ .... $.ajax() .... })(jQuery); (also see here JavaScript / jQuery closure function syntax)

Adding jQuery to Magento

Change your layout XML file createadmincontroller.xml:

<?xml version="1.0"?>
<layout version="0.1.0">
    <adminhtml_custom_index>
        <reference name="head">
            <action method="addItem">
                <type>skin_js</type>
                <script>js/jquery-1.x.x.js</script>
            </action>
            <block type="core/text" name="jquery.noconflict">
                <action method="setText">
                    <text><![CDATA[<script type="text/javascript">var $j = jQuery.noConflict();</script>]]>
                    </text>
                </action>
            </block>
        </reference>
        <reference name="content">
            <block type="adminhtml/template" name="createadmincontroller" template="createadmincontroller/index.phtml" />
        </reference>
    </adminhtml_custom_index>
</layout>

Also see: https://magento.stackexchange.com/a/53905/3326

Community
  • 1
  • 1
7ochem
  • 2,183
  • 1
  • 34
  • 42
  • Alternatively you can shorten the use to $j.ajax() because of the `var $j = jQuery.noConflict();` in `js/lib/jquery/noconflict.js` :) – β.εηοιτ.βε Jan 30 '15 at 09:03
  • @7ochem now getting "jQuery is not defined" – Priyank Jan 30 '15 at 09:37
  • @7ochem also i already used no conflict but still getting the error. – Priyank Jan 30 '15 at 09:38
  • @b.enoit.be i already used no conflict but still getting the error. – Priyank Jan 30 '15 at 09:42
  • @Priyank what is your version of Magento ? jQuery only comes recently bundled in Magento, so maybe you are in a version where you don't have it ? – β.εηοιτ.βε Jan 30 '15 at 09:43
  • @b.enoit.be i have ver 1.7.0.2.so how to include new jquery in my module.becoz when ever in include jquery in my .pthml file like this "" it throw an error.plz help in this – Priyank Jan 30 '15 at 09:47
  • @7ochem can you explain how to add for js for particular page as mention here "http://www.magesolution.com/blog/how-to-add-jquery-in-magento/". i want to add js in my senario.plz explain – Priyank Jan 30 '15 at 10:41
  • @Priyank, I've updated my answer to explain how to add jQuery – 7ochem Jan 30 '15 at 14:30
  • @7ochem hey!! thanks for your update.but still not working and also now my admin module page looks blank.may be i did some thing wrong.please explain me or if free come for chat :) – Priyank Feb 02 '15 at 05:03
  • @7ochem sorry dude.it's my fault.your code is works absolutely fine :) i accept & upvote your answer.thanks :) – Priyank Feb 02 '15 at 05:15