How to get an array in Joomla (2.5 / 3.x)
<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 elements into the $ items array?
If you just need all the elements, the Joomla path will be:
$items = JRequest::getVar('item', array()); where the second parameter will be your default if the "item" is not set. But note that this selects the parameters using the name, as usual.
The same with the Joomla 11.1 platform and above will be:
$items = $app->input->get('item', array(), 'ARRAY'); The third parameter is needed here, since the default filter is 'cmd', which does not allow arrays. Additional information in the documents .
If you use JForm to create forms, you need to extract the published data from the jform array.
For built-in 3.x components, the code will look inside the controller, for example:
// Get POSTed data $data = $this->input->post->get('jform', array(), 'array'); where $this->input is the input object inherited from JControllerBase .
For components using legacy MVC classes , the code will look like this:
// 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. how
$result = (array) $source; The data array itself is NOT processed.