Rasmus Bech
Rasmus Bech

Reputation: 84

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!

Upvotes: 1

Views: 5506

Answers (2)

Panagiotis
Panagiotis

Reputation: 1

I just resolved an issue like that in Joomla. What I did is simple and works fine.

1) I created an article with a form inside the html editor of the article.

2) in this html code of the article, for the form I gave form method="POST" action="child.php".

3) Now, child.php is a page inside my joomla installation folder that has an html page exactly as my template layout and links and article etc. So, I made child.php a page that looks like as any other page on my website, but also with a form inside it.

4) Inside this child.php there is a form. Then the field I want to populate from the article with the form inside it i gave the value="?php echo $_POST['VARIABLE']; ?", where VARIABLE is the name of the field of my html form article.

So, this is it, now my article html form redirects me to child.php page and child.php is populated with the variable of my previous html article.

Upvotes: 0

MrCode
MrCode

Reputation: 64526

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; ?>

Upvotes: 2

Related Questions