dongqu9917 2014-03-15 07:03
浏览 44
已采纳

获取joomla组件控制器功能中的值

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.

  • 写回答

1条回答 默认 最新

  • dszm02606009 2014-03-16 15:32
    关注

    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>       
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line