<form>
<input type="checkbox" name="item[]" value="1" />
<input type="checkbox" name="item[]" value="2" />
<input type="checkbox" name="item[]" value="3" />
</form>
<?php
$app = JFactory::getApplication();
$items = $_POST['type']; // This works but is not Joomla wise...
$items = $app->input->getArray(array('type_ids')); // Tried multiple ways but can't get it to work.
?>
What should be the correct way to load all form items into an array $items?
If you just want all the items, the Joomla way would be:
$items = JRequest::getVar('item', array());
where the second parameter would be your default value if 'item' is not set. But note that this fetches the params via the name, just as usual.
The same using the Joomla Platform 11.1 and above would be:
$items = $app->input->get('item', array(), 'ARRAY');
Here the third parameter is necessary since the default filter is 'cmd' which does not allow arrays. More information in the docs.
If you are using JForm to make forms, you need to extract the posted data from the jform array.
For the native 3.x components the code will look inside the controller like:
// Get POSTed data
$data = $this->input->post->get('jform', array(), 'array');
where $this->input
is the input object, inherited from JControllerBase
.
For the components using legacy MVC classes, the code will be:
// Get input object
$jinput = JFactory::getApplication()->input;
// Get posted data
$data = $jinput->post->get('jform', array(), 'array');
Security notice:
ARRAY - Attempts to convert the input to an array. Like
$result = (array) $source;
The data array itself is NOT sanitized.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With