Question

iam struggeling with two submit buttons in my component form.

<button type="submit" class="button"><?php echo JText::_('Save1'); ?></button>
<input type="hidden" name="option" value="com_mycomponent" />
<input type="hidden" name="task" value="data.save_1" />

<button type="submit" class="button"><?php echo JText::_('Save2'); ?></button>
<input type="hidden" name="option" value="com_mycomponent" />
<input type="hidden" name="task" value="data.save_2" />

The problem ist, each button leads to the controllers function save_2.

If iam changing the order to

<button type="submit" class="button"><?php echo JText::_('Save2'); ?></button>
<input type="hidden" name="option" value="com_mycomponent" />
<input type="hidden" name="task" value="data.save_2" />

<button type="submit" class="button"><?php echo JText::_('Save1'); ?></button>
<input type="hidden" name="option" value="com_mycomponent" />
<input type="hidden" name="task" value="data.save_1" />

both buttons are executing the function save_1 in the controller.

It is always executing only the task of the last button. What is wrong here? I would like to execute controllers task1 when I use the button task1, and execute controllers task2 when I use the button task2.

thx Perino

Was it helpful?

Solution

Thank you! I did it now on a similar way:

in the view (i worked with different sub-layouts in this view), id did now

<button type="submit" class="button" name="save_1"><?php echo JText::_('Save1'); ?></button>
<input type="hidden" name="option" value="com_mycomponent" />
<input type="hidden" name="task" value="data.save" />

<button type="submit" class="button" name="save_2"><?php echo JText::_('Save2'); ?></button>
<input type="hidden" name="option" value="com_mycomponent" />
<input type="hidden" name="task" value="data.save" />

Then in the controller i did

public function save()
{
If (Isset($_POST['save_1']))
{
echo "Button 1 saved"; // here you can do your task for button1
}
If (Isset($_POST['save_2']))
{
echo "Button 2 saved"; //here you can do your task for button2
}

I hope this can also help anybody with same problem.

OTHER TIPS

it looks like both your submit-buttons are in the same form. So the last taks - input will overwrite the first. So either you need to make two forms (probably not what you want?) or you need to handle the click-event depending on which submit-button is clicked, and update the task-variable accordingly, something like:

<button type="submit" class="button"><?php echo JText::_('Save2'); ?></button>
<input type="hidden" name="option" value="com_mycomponent" />
<input type="hidden" id="formtask" name="task" value="data.save_2" />

<button type="submit" class="button" 
onclick="document.getElementById('formtask').value='data.save_1'">
<?php echo JText::_('Save1'); ?></button>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top