#joomla #php

Joomla get POST data from form using jinput

As from Joomla! 1.5 retrieving POST data through JRequest::getVar('var_name'); or JRequest::get('post'); is deprecated. The new approach to get the whole $_POST array :

$input = JFactory::getApplication()->input;
$post_array = $input->getArray($_POST);

Retrieving single value from $_POST:

$input = JFactory::getApplication()->input;
$my_val = $jinput->get('my_val');

The Jinput class provides the capability of filtering data. Avaible filters are:

  • INT, INTEGER – Matches the first, signed integer value.
  • UINT – Matches the first unsigned integer value.
  • FLOAT, DOUBLE – Matches the first floating point number.
  • BOOL, BOOLEAN – Converts the value to a boolean data type.
  • WORD – Allows only case insensitive A-Z and underscores.
  • ALNUM – Allows only case insensitive A-Z and digits.
  • CMD – Allows only case insensitive A-Z, underscores, periods and dashes.
  • BASE64 – Allows only case insensitive A-Z, forward slash, plus and equals.
  • STRING – Returns a fully decoded string.
  • HTML – Returns a string with HTML entities and tags intact, subject to the white or black lists in the filter.
  • ARRAY – Returns the source as an array with no additional filtering applied.
  • PATH – Matches legal characters for a path.
  • USERNAME – Strips a select set of characters from the source (\x00, -, \x1F, \x7F, <, >, “, ‘, %, &).

There are two basic ways of using filters, for example:

$id = $input->getInt('id');

and

$id = $input->get('id',null,'int');

Above examples have the same effect. In the $id we get an id from the request ensuring, that it is an integer.

As an bonus here is an example of parsing form fields wrapped in the jform array.

// Required objects 
$input = JFactory::getApplication()->input; 

// Get the form data 
$formData = new JRegistry($input->get('jform', '', 'array')); 

// Get any data being able to use default values 
$id = $formData->get('id', 0); 
$name = $formData->get('name', null); 
$email = $formData->get('email', null);

This one was found on phproberto.com

More on Jinput Class could be found in Joomla documentation or in this Joomla manual.

When working on custom components You’ll probably like the below snippet:

//Joomla 2.5

$jinput = JFactory::getApplication()->input;
$rg=array();
$rq['option'] = $jinput->getCmd('option', '');
$rq['view']   = $jinput->getCmd('view', '');
$rq['layout'] = $jinput->getCmd('layout', '');
$rq['task']   = $jinput->getCmd('task', '');
$rq['itemid'] = $jinput->getCmd('Itemid', '');

var_dump($rq);

Stealed it from here: https://snipt.net/raw/7bae96ea4a6a97920f4aecbcd0f29308/?nice