我面临的问题(因为我对Joomla组件开发相对较新)关于如何在我的Joomla Custom Component的controller.php中的两个函数之间传递值
以下是我的controller.php文件
<?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**
}
}
我在控制器的Display()任务中有两个表单字段,我想获取我在另一个名为displayData()的任务中的那些字段中输入的值。 我想出的唯一方法是使用form的post方法并通过$ _POST []方法获取变量。 Joomla提供用户状态和变量以及所有这些东西,但我无法弄清楚如何在这里使用它们。
答案 0 :(得分:2)
首先我注意到你的表单有表格标签因此无法向服务器发送数据。更改代码如下。
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>';
}
请注意,您可以更清晰地使用MVC。 MVC是一种安排代码以便更好地开发和调试的形式。您可以创建以下结构。
com_helloworld
在com_helloworld-&gt; controllers-&gt; helloworld.php下放这个。
<?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**
}
}
在com_helloworld-&gt; view-&gt; tmpl-&gt; default.php下放这个。
<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>