Search code examples
phpmodel-view-controllerjoomlajoomla-extensionsjoomla-component

joomla pass variable between views


I'm in the middle of programming a joomla component, this component will have a multiple step form for registration, the first one containing 2 radio buttons. What is the easiest approach to pass the variable from the radiobutton on to step2? when the user submits the first form the controller is called to display the second step, is there an easier approach to this? Thanks in advance.

Code:

step1 of the form (view code):

<form name="step1" action="<?php echo JRoute::_('index.php option=com_mycomponent&task=mycomponent.register'); ?>" method="POST">
<input type="radio" name="placement" value="0" checked /> Option 1<br />
<input type="radio" name="placement" value="1" /> Option 2<br />
<div class="next" onClick="document.forms['step1'].submit();"></div>
</form>

Controller code to handle mycomponent.register

class mycomponentControllermycomponent extends JController

{

    public function register()

    {
    $this->setRedirect(JRoute::_('index.php?option=com_mycomponent&view=default&layout=step2', false));
    }

}

I would like to pass the value from the radio to step2 ;)

Hope i didn't miss a topic concerning this already, couldn't find one atleast!


Solution

  • In the register method you can access the radio value and then pass it in the link to your view:

        public function register()
        {
            $placementOption = JRequest::getInt('placement', 0, 'post');
    
        $this->setRedirect(JRoute::_('index.php?option=com_mycomponent&view=default&layout=step2&placement=' . $placementOption, false));
        }
    

    In your view of step 2 (view.html.php), you can then access the option in the same way (this time the value is in GET not POST):

    $placementOption = JRequest::getInt('placement', 0, 'get');
    
    $this->assignRef('placement', $placementOption);
    

    Finally, if you want to access it in the views template (e.g. default.php)

    <?php echo $this->placement; ?>