2

By using var_dump($_POST);, I'm getting the following output:

array(2) 
{
    ["data"] => array(2) 
    {
        ["title"] => "XXX"
        ["server"] => "XXXXXX"
    }
    ["Itemid"] => "101"
}

My aim is to get the server value. I can easily achieve this by using:

$_POST['data']['server'];

Having snooped around, I found that using the following can be used:

$array  = $input->post->getArray(array());      
$server = $array['data']['server'];

Is this the best approach for JInput or is there something better?

5

This should work:

$values = $app->input->getArray(array('data'=>'array'));
echo $values['data']['server'];

Please see I did not mention post variable specifically, because this can retrieve both get and post values. If you need to get a value from a specific super global, such as post, you can use something like $app->input->post

You can retrieve multiple values by passing their names and data type in above format. This way you can get exactly what you want instead of getting raw data which is potentially not safe. Always mention the data type that you require.

| improve this answer | |
4

You could try:

$data = $input->post->get('data', array(), 'ARRAY');
$server = $data['server'];

Haven't a playground to test this in, but I think it works.

| improve this answer | |
  • Isn't this method used in an older version of the platform? – Lodder Jun 2 '15 at 15:58
  • Not 100% sure, but the method is in the current source, since 11.1, with no deprecated notice. – codinghands Jun 2 '15 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.