0

I'm pretty new to magento, so I've been in the process of adding a custom text box in the admin panel that has to query the mysql db to retrieve a specific value.

So I'm thinking that ajax would pretty much do the job with that, however for some reason when i do the ajax call, and step through the code using the debugger, i notice that the value being sent through never shows up in the function of the block class.

phtml code:

new Ajax.Request('<?php echo $this->ajaxCheck(); ?>', {
    method:'get',
    parameters:{test_attrib:val},
    onSuccess: function(transport){
        retval = transport.responseText;
   }
});

Block class code:

public function ajaxCheck()
{
    try {
        $tval = $this->getRequest()->getParam("test_attrib");

        if (empty($tval)) {
            Mage::throwException($this->__('Invalid val('.$tval.'). Please check & resubmit.'));
        }
        ....do something with $tval.
        print_r($tval);
    }catch(Exception $e) {
             echo $e->getMessage();
    } 
}  

If anyone could help with this please.


Thanks for the quick reply guys!

@Raphael at Digital Pianism

As you advised, I made the following changes:

Controller:

class Ctest_Mydev_AjaxControl extends Mage_Adminhtml_Controller_Action
 {
public function AjaxAction(){

    $tval = $this->getRequest()->getParam("test_attrib");

      ..do something with tval.

    $result['retval'] = $tval;
      $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
}}

in phtml file function:

  var url = '<? echo Mage::helper('adminhtml')->getUrl('ctest/mydev/ajaxcontrol/AjaxAction'); ?>';

 new Ajax.Request(url, {
                method:'get',
           parameters:{test_attrib:val},
           onSuccess: function(transport){

        var response = transport.responseText.evalJSON();
        retval = response.retval;
           }
        });

like I've mentioned in the comment,when I run it through the debugger, I notice that a key value is assigned at the end of the url: ctest/mydev/ajaxcontrol/AjaxAction

and after that nothing appears to happen. I'm am unsure what it is that I could be missing

  • You need to create controller for that.It will not work in block class – Minesh Patel Mar 16 '16 at 9:05
0

Well the problem here is you're trying to call an URL that matches a block function.

What you need to do:

  • Declared a controller first
  • Create the ajaxAction method in your controller
  • Use $this->getUrl('frontname/controller/action'); to call your controller action in your JS

In your controller you can then return the result by doing

$result['retval'] = $tval;
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));

And in your JS you can retrieve the value:

var response = transport.responseText.evalJSON();
retval = response.retval;
  • alright i did that, and it seems to assign a key value to the url, however, it seems to do nothing afterwards. When i try to access that url directly through the browser, i get a 404 – u_jansen Mar 16 '16 at 14:21
  • @u_jansen see my edited answer ;) – Raphael at Digital Pianism Mar 16 '16 at 14:27
  • I edited my initial question – u_jansen Mar 17 '16 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.