Get URL-Parameter in Controller

281 views
Skip to first unread message

Timotheus Titus

unread,
Feb 24, 2014, 3:28:52 AM2/24/14
to joomla-de...@googlegroups.com
Dear Joomla-Developers :-)


I'm trying to develop my first component for Joomla 3.x.

In the backend almost everything works fine but in the frontend there is one big problem:

The frontend shows a view called "items". If a user picks an item from this list he gets redirected to the view "item" (detail-view: index.php?option=com_mycomponent&view=item&id=4).
If the user wants to create a comment on this item, he has to click a button, which redirects the user to the comment-view (index.php?option=com_mycomponent&view=comment&id=4).

The data the user enters in this field have to be saved to the database, using the table, which is located in the backend.

That works already: I'm able to save the comment itself, the user-id, the timestamp etc. but I'm not able to save the given ID, which allows me to assign the comment to the item.

I thought I could just get the URL-Param "ID" by using this in my frontend-controller, which saves the data to the database:


$item_id = JRequest::getVar('id');


But this does never have a value. What do I have to use here?

Cliff Ford

unread,
Feb 24, 2014, 5:24:11 AM2/24/14
to joomla-de...@googlegroups.com
Put the id in a hidden field in the comment form - and do not confuse
the item_id with the id of the comment.

Cliff

Timotheus Titus

unread,
Feb 24, 2014, 8:12:46 AM2/24/14
to joomla-de...@googlegroups.com
It works - but there is one more question:


I created a comment.xml like this:

<form>
<fieldset name="comment">
<field name="id" type="hidden"/>
<field name="item_id" type="hidden"/>

<field name="firstname" type="text" size="40"
class="inputbox"
required="true"
default=""
autocomplete="off"
label="COM_BESTIA_BESTIA_TYPEDETAILS_INFO"
description="COM_BESTIA_BESTIA_TYPEDETAILS_DESC"/>

<field name="lastname" type="text" size="40"
class="inputbox"
required="true"
default=""
autocomplete="off"
label="COM_BESTIA_BESTIA_TYPEDETAILS_INFO"
description="COM_BESTIA_BESTIA_TYPEDETAILS_DESC"/>
</fieldset>
</form>


In my default.php (view) I'm setting the value:

$item_id = JFactory::getApplication()->input->getInt('item_id');
$mod_item_id = $this->form->setValue('item_id', null, $item_id);

That works so far :)


Well, now I want to split up the comment.xml into different fieldsets, for example:

<?xml version="1.0" encoding="UTF-8"?>

<form>
<fieldset name="comment">
<field name="id" type="hidden"/>
<field name="item_id" type="hidden"/>


</fieldset>
<fieldset name="personal-data">
<field name="firstname" type="text" size="40"
class="inputbox"
required="true"
default=""
autocomplete="off"
label="COM_BESTIA_BESTIA_TYPEDETAILS_INFO"
description="COM_BESTIA_BESTIA_TYPEDETAILS_INFO_DESC"/>

<field name="lastname" type="text" size="40"
class="inputbox"
required="true"
default=""
autocomplete="off"
label="COM_BESTIA_BESTIA_TYPEDETAILS_INFO"
description="COM_BESTIA_BESTIA_TYPEDETAILS_INFO_DESC"/>
</fieldset>
</form>

But now it doesn't work at all: The item_id is not set in the controller and the fields, which are located in the fields are not written into the database.


What is the problem here?

Cliff Ford

unread,
Feb 24, 2014, 9:14:49 AM2/24/14
to joomla-de...@googlegroups.com
I don't think we have enough information to diagnose the problem. Which
version of Joomla are you using? You don't need to put the hidden fields
in a separate fieldset. Turn on debeugging in the Global Configuration
and look for the query that saves the form data to the database. Check
that actually works with phpMyAdmin (also, you can turn on php error
reporting in a development environment). And of course, look at the
source of the form to check the hidden fields actually have the values
you expect.

Cliff

Timotheus Titus

unread,
Feb 24, 2014, 9:49:44 AM2/24/14
to joomla-de...@googlegroups.com
Hi Cliff,

thank you for your response :)

Well, I know that I don't have to use a seperate fieldset for the item_id and id-hidden-fields, but I want to structurize my view and load the fieldsets seperately on different positions.


It's Joomla 3.2.2.

I'll have a look at the debugging, thanks so far. If I have any further questions I'd like to post it here, too (your postings were very informative and friendly :) ).


Thanks :)

Viper

unread,
Feb 24, 2014, 10:02:22 AM2/24/14
to joomla-de...@googlegroups.com
You can use fields block to separate fieldset blocks.

<?xml version="1.0" encoding="utf-8"?>
<form>
    <fields name="movie">
        <fieldset name="edit">
...


This is useful if you want to use one form for different datasets(of course in this situation you need to additional body movements to fill form with data from set and for displaying forms). If you need just separate fieldsets so you can use getFieldset() and when process each field via foreach().

<?xml version="1.0" encoding="utf-8"?>
<form>
    <fieldset name="edit">
        <field ...
    </fieldset>
    <fieldset name="edit1">
        <field ...
    </fieldset>
    <fieldset name="edit2">
        <field ...
    </fieldset>
    ...
</form>



<fieldset class="form-horizontal">
    <legend><?php echo JText::_('COM_SETTINGS_AP_GLOBAL_LABEL'); ?></legend>
    <?php foreach ($this->form->getFieldset('edit') as $field): ?>
        <div class="control-group">
            <div class="control-label"><?php echo $field->label; ?></div>
            <div class="controls"><?php echo $field->input; ?></div>
        </div>
    <?php endforeach; ?>
</fieldset>

<fieldset class="form-horizontal">
    <legend><?php echo JText::_('COM_AP_GLOBAL1_LABEL'); ?></legend>
    <?php foreach ($this->form->getFieldset('edit1') as $field): ?>
        <div class="control-group">
            <div class="control-label"><?php echo $field->label; ?></div>
            <div class="controls"><?php echo $field->input; ?></div>
        </div>
    <?php endforeach; ?>
</fieldset>

...
Reply all
Reply to author
Forward
0 new messages