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
- helloworld
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** } }
- controllers
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>