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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?