6
<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?

1
  • why not use the checkboxes type if you want the array? Commented May 3, 2013 at 12:39

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Well, 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.
4

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.

1 Comment

lol, 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..
1

For the components using legacy the following code works (Version 3.3):

 $jinput = JFactory::getApplication()->input; $data2 = $jinput->post->getArray(array()); var_dump($data2); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.