Joomla - Component: Adding Comment (Create Form)



After create table in database, now we build form which visitor will leave comment. This form will be loaded when single posting displayed. We involve session to make this form more advance. When they logged, their name will display at the form.

  1. Open "hello.html.php" file within joomla/components.

    Enter following function:

    	function showCommentForm($option, $hello_id, $name)
    	{
    	?>
      <br /><br />
      <form action="index.php" method="post">
      <table>
        <tr>
          <td>
          <strong>Name: </strong>
        </td>
        <td>
          <input class="text_area" type="text" 
    name="full_name" id="full_name" 
    value="<?php echo $name; ?>" />
        </td>
      </tr>
      <tr>
        <td>
          <strong>Comment:</strong>
        </td>
        <td>
          <textarea class="text_area" cols="20" rows="4" 
    name="comment_text" id="comment_text" 
    style="width:500px"></textarea>
        </td>
        </tr>
      </table>
      <input type="hidden" name="hello_id" 
    value="<?php echo $hello_id?>" />
      <input type="hidden" name="task" 
    value="comment" />    
      <input type="hidden" name="option" 
    value="<?php echo $option?>" />        
      <input type="submit" class="button" 
    id="button" value="Submit" />    
      </form>
        <?php
    	}
    
  2. Open "hello.php" within joomla/components. Enter

    following bold code in viewHello():

    function viewHello($option)
    {
      $id = JRequest::getVar('id', 0);
      $row =& JTable::getInstance( 'hello', 'Table');
      $row->load($id);
    	
      if(!$row->published)
      {
        JError::raiseError( 404, JText::_('Invalid ID Provided'));		
      }
      HTML_hello::viewHello($row, $option);
    
      $user =& JFactory::getUser();
      if($user->name)
      {
        $name = $user->name;
      }
      else
      {
        $name = '';
      }
      HTML_hello::showCommentForm($option, $id, $name);
    }
    

When you access, for example like this http://localhost/Joomla/hello-world/view/2, the form comment loaded.



Series this article:
Joomla - Component: Create Hello World Component on Front End
Joomla - Component: Create Hello World Component on Back End
Joomla - Component: Registering Your Component in database
Joomla - Component: Create Component Link for Front End
Joomla - Component: Creating Toolbars
Joomla - Component: Available Toolbar Buttons
Joomla - Component: Creating the Database Table
Joomla - Component: Creating Form Input Data at Back End
Joomla - Component: Saving Data to Database
Joomla - Component: Creating a List Screen
Joomla - Component: Creating Advance List Screen
Joomla - Component: Editing Data
Joomla - Component: Deleting Data
Joomla - Component: Updating publish state
Joomla - Component: Hello From Database at Front End
Joomla - Component: a Hello page
Joomla - Component: Generating Friendly Links
Joomla - Component: Adding Comment ( Create Table )
Joomla - Component: Adding Comment (Create Form)
Joomla - Component: Adding Comment ( Insert Data )
Joomla - Component: Adding Comment ( Displaying Comments )
Joomla: Fast Road to Understand Component Programming


Tag: joomla, cms Category: Post : November 13th 2007 Read: 11,284 Bookmark and Share

blog comments powered by Disqus