How to get an array the correct way in Joomla (2.5/3.x)
Solution 1
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.
Solution 2
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.
Solution 3
For the components using legacy the following code works (Version 3.3):
$jinput = JFactory::getApplication()->input;
$data2 = $jinput->post->getArray(array());
var_dump($data2);
COBIZ webdevelopment
Updated on June 18, 2022Comments
-
COBIZ webdevelopment almost 2 years
<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?
-
Mohd Abdul Mujib about 10 yearslol, thats not what I meant(thanks though), it was the obvious[bit.ly/1dyYYiA], but what I meant was about the 'For the native 3.x components' part, there seems to be no mention of that..
-
Valentin Despa over 9 yearsWell, no! ARRAY will just make sure you get a valid PHP array object. But it does not actually clean the input. You need to do that separately.