0

Send data from controller to Phtml in Magento 1.

I use ajax send data from phtml to controller and handle it. After processing, I want to send the data back to phtml. And put it on

<option value = "data">

Ex: My data in controller is ABC

I want to put it in like this

<option value = "ABC">
3
  • So do you want to fill the dopr-down options from your result. right? Commented Aug 16, 2019 at 5:30
  • yes, can you help me
    – Brian N
    Commented Aug 16, 2019 at 6:02
  • can youplease share the code of your phtml ajax request. and controller responce. Commented Aug 16, 2019 at 6:20

1 Answer 1

2
  • You need to create an controller action and return JSON data (In your case return whole html for select element) from the controller.
  • Use Ajax to call the Controller action and replace the Select options html with the new html from controller using jQuery.

Sample Controller Action Code

public function gethtmlAction(){
  $request = $this->getRequest()->getParam('request');
  // Your Logic
  $responseHtml = ""; // Html content
  $jsonData = json_encode(compact('responseHtml'));
  $this->getResponse()->setHeader('Content-type', 'application/json');
  $this->getResponse()->setBody($jsonData);
}

Sample jQuery code in phtml

jQuery.ajax({
             url : '<?php echo $this->getUrl('module/controller/getHtml',array('_secure'=>true));?>',
             dataType : 'json',
             type : 'POST',
             data: {request:request1},
             success : function(data){
                 jQuery('#select-id').html(data.response['responseHtml']);
                }
            });

Hope it finds you helpful.

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.