Question

I am facing issue (as i am relatively new to Joomla Component Development) regarding how to pass values between two functions in a my Joomla Custom Component's controller.php

Below is my controller.php file

 <?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla controller library
jimport('joomla.application.component.controller');
JHtml::script(Juri::base() . 'templates/googlemaps/navigation.js');
/**
 * Hello World Component Controller
 */
class HelloWorldController extends JControllerLegacy
{
    function display(){
   echo '<form action="'. echo JRoute::_('index.php?option=com_helloworld&controller=helloworld&task=displayData') .'" method="post">';
   echo '<h1>This is Insert Task</h1><br><input type="text" id="name" placeholder="Name"><br>';
   echo '<input type="text" id="surname" value="surname" name="surname" placeholder="Surname">';
   echo '<input type="submit" value="submit">'; 
   echo '</form>';
    }

    function delete(){
        $id=JRequest::getVar('id');
        echo "You want to delete $id";
    }

    function displayData(){

        $name=JRequest::getVar('name');
        $surname=JRequest::getVar('surname');
        //$mainframe =& JFactory::getApplication();
        //$stateVar = $mainframe->getUserStateFromRequest( "$option.state_variable", "surname", "Hello" );
        echo "Your name is $name and surname is $surname";
        //**How can i get the name and surname variables here after the page refreshes and this task loads**
    }
}

I have two form fields in the Display() task of controller and i want to get the values i enter in those fields in another task below named displayData(). The only way i figured out is using the post method of form and getting the variables via $_POST[ ] method. Joomla provides user state and variables and all that stuff but i m not able to figure out how to use them here.

Was it helpful?

Solution

First i have noticed you form does have form tag therefore it can not send data to server.Change the code as follows.

    function display(){
       echo '<form action="'. echo JRoute::_('index.php?option=com_helloworld&controller=helloworld&task=displayData') .'" method="post">';
       echo '<h1>This is Insert Task</h1><br><input type="text" id="name" placeholder="Name"><br>';
       echo '<input type="text" id="surname" value="surname" name="surname" placeholder="Surname">';
       echo '<input type="submit" value="submit">'; 
       echo '</form>';       
    }

Note you can use MVC for much clarity. MVC is a form of arranging you code for better development and debugging. You can create the following structure.

  • com_helloworld

    • controllers
      • helloworld.php
    • models
      • helloworld.php
    • views
      • helloworld
        • view.html.php
        • tmpl
          • default.php

    Under com_helloworld->controllers->helloworld.php put this.

     <?php
    // No direct access to this file
    defined('_JEXEC') or die('Restricted access');
    
    // import Joomla controller library
    jimport('joomla.application.component.controller');
    JHtml::script(Juri::base() . 'templates/googlemaps/navigation.js');
    /**
     * Hello World Component Controller
     */
    class HelloWorldController extends JControllerLegacy
    {
    
        function delete(){
            $id=JRequest::getVar('id');
            echo "You want to delete $id";
        }
        function displayData(){
            // LOAD THE MODEL AND DO PROCESSING THERE
            // $model = $this->getModel();
    
            $name=JRequest::getVar('name');
            //$surname=JRequest::getVar('surname');
            //$mainframe =& JFactory::getApplication();
            //$stateVar = $mainframe->getUserStateFromRequest("$option.state_variable", "surname", "Hello" );
            echo "Your name is $name and surname is $surname";
            //**How can i get the name and surname variables here after the page refreshes and this task loads**
       }
    
    }
    

Under com_helloworld->view->tmpl->default.php put this.

    <form action="<?php echo JRoute::_('index.php?option=com_helloworld&controller=helloworld&task=displayData') ?>" method="post">
    <h1>This is Insert Task</h1><br><input type="text" id="name" placeholder="Name"><br>
    <input type="text" id="surname" value="surname" name="surname" placeholder="Surname">
    <input type="submit" value="submit"> 
    </form>       
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top