1

Hello World, I will jump right into it..

Desired Logic

I would like my template to dispatch an event every time the user clicks the
on and off buttons. My view.phtml file look likes this...

/*Some Html*/ 


<?php
/*=== Variables ===========*/
$customer = Mage::getModel('customer/session')->getCustomer();
/*$groupId = Mage::getSingleton('customer/session')->getCustomerGroupId();*/
$groupId = $customer->getGroupId();
$group = Mage::getModel('customer/group')->load($groupId);
$groupCode = $group->getCode();

$OnArray = array(
   "value" => "on", 
   "customer" => $customer
    );
$OffArray = array(
   "value" => "off", 
   "customer" => $customer
    );
?>

  /* More HTML * /  

<form action="">
    <input type="radio" name="auto-refill" value="On" onClick = "<?php Mage: dispatchEvent('autoFill_Update', $OnArray ?>" />On<br>
    <input type="radio" name="auto-refill" value="Off" onClick = "<?php Mage: dispatchEvent('autoFill_Update', $OffArray ?>" />Off
</form>

Update
Based on the answers and comments I updated my template code..
I am still having trouble calling the controller from the view..
Code Update

<script type="text/javascript">
 function autoRefillController(){
           new Ajax.Request("<?php echo $this->getUrl('Autorefill/CustomerGroup/updateCustomerGroup') ?>", {
               method: 'post',
            //   parameters: {parameter_name:value},
               onSuccess: function(transport) {

                   //alert(transport.responseText);
                   alert('response for ON received');
               }
           });
       }
</script>

<form action="">
    <input type="radio" name="auto-refill" value="On" onClick = "autoRefillController()"/>On<br>
    <input type="radio" name="auto-refill" value="Off" onClick="dispatchCustomEvent()"/>Off
</form>
<script type="text/javascript">
function dispatchCustomEvent() {
    //data.eventName = event;

    //new Ajax.Request('module/controller/action', {
    new Ajax.Request("<?php echo $this->getUrl('Autorefill/CustomerGroup/updateCustomerGroup') ?>", {
        method: 'post',
        //parameters: data,
        onSuccess: function(transport) {
            alert('response for OFF received');
        }
    });
}
</script>

Questions
Did I access my controller correctly?

Any guidance is truly appreciated

1
1

I think you're confusing the use cases for the Magento event/observer system. Registering and triggering events with Mage::dispatchEvent is typically reserved for server-side activities, prior to the output/rendering process.

Judging by your sample code, the onClick assignment will do nothing on click, as you're executing PHP code at run-time and it will not run when the element is clicked by the user.

Look into using JavaScript events instead:

Where the click handler might look like this instead:

onClick="dispatchCustomEvent('autoFill_Update', <?php echo Mage::helper('core')->jsonEncode($data)) ?>;"

Where dispatchCustomEvent might look like this:

function dispatchCustomEvent(event, data) {
    data.eventName = event;

    new Ajax.Request('module/controller/action', {
        method: 'post',
        parameters: data,
        onSuccess: function(transport) {
            alert('response received');
        }
    });
}

Where module/controller/action is the endpoint in Magento to match your controller to receive this event and do something with the event data.

2
  • Thanks for the answer. I still can not access my controller............Is my ajax request logic correct ? Ajax.Request("<?php echo $this->getUrl('Autorefill/CustomerGroup/updateCustomerGroup') ?>"
    – Rstack
    Dec 24 '14 at 5:15
  • I'm not sure. If you could post your module's config.xml, I could get a better idea. A snippet of the controller class would help, too. <-- though this might be more well suited as a separate question (else research controller setups yourself). For starters, the module/controller part of the route should be lowercase, while the action part should match the controller's method name exactly. If the controller's method is updatesomeThingAction then the action part of the route would be updatesomeThing. Dec 25 '14 at 1:57
0

you would like to do somethings when user click the on or off button, so, dispatch event is not better for this case, so just hit an ajax call when user click the on or off button,then in controller method do whatever you want based on the user request..

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.