0

In controller file I put this,

$result = "success";
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));

How to fetch output in phtml file?

8
  • what exact data u want to return to phtml please specify more details Commented May 26, 2017 at 11:21
  • @NAGARAJUKASA - How to get controller files result in phtml file?. I have phtml file but how to call the result. Commented May 26, 2017 at 11:22
  • You should call this controller using ajax from phtml then you will get that response
    – Amit Bera
    Commented May 26, 2017 at 11:23
  • @AmitBera - In phtml file I should directly call $result then it will display the result? Commented May 26, 2017 at 11:25
  • 1
    let me know whether you got proper solution for u r question? Commented May 29, 2017 at 5:08

2 Answers 2

0

Here i am showing you sample example using ajax request with json responsce

Template file: /app/design/frontend/rwd/default/template/pay/index.phtml

<div id="results" style="width:100px; height: 200px;border:1px solid red;">
</div>

<button type="button" id="id1" class="myButton">Click Me 1!</button>
<button type="button" id="id2" class="myButton">Click Me 2!</button>

<script>
    jQuery(document).ready(function() {
          jQuery(".myButton").click(function() {
            var id = this.id;
            jQuery.ajax({      
            type: "POST",
            data: 'pid=' + id,
            dataType: 'JSON',
            url: "<?php echo Mage::getBaseUrl().'pay/index/sampledata' ?>",
            success:function(response){                       
                if (response){

                 jQuery('#results').html(response.pid);

                }
            }

            });
        });
     });
</script>

IndexController.php /app/code/local/Excellence/Pay/controllers/IndexController.php

 public function sampledataAction() 
 {
     $post = $this->getRequest()->getPost();
     $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($post));

 }

Note: Please try this code in default magento and custom module. thanks

0

Using the phtml file you can call the controller via ajax you just need the controllers url and then can use ajax to get the data.

$.ajax({
  url: "/route/index/controller",
  success: function(response) {
                var result = jQuery.parseJSON(response);
  },
  error: function(xhr) {
  }
});
//do stuff with response

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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