3

im trying to send an array from javascript to Joomla 3.x php file.

var options = ['foo', 'bar'];

$.post('index.php?option=component&view=componentview&Itemid=123&tmpl=component&layout=xlsx', {'xlsx_options': options})

Then, I have a php file which creates data in order to download xlsx file. But I need to filter by this options array and Im not able to retrieve it. I've tried this without success.

$xlsx = $_POST['xlsx_options'];
$xlsx = json_decode($_POST['xlsx_options'], true);
$xlsx = JFactory::getApplication()->input->get('xlsx_options');

Any help? Thanks!

  • Do you need anymore help or was Lodder's answer the right one for you? – Dennis Heiden Aug 25 '17 at 13:37
  • @Mark your question appears to be abandoned. If Lodder's answer solved your issue please award it the green tick. If there is still something to resolve, please leave a comment under the answer to isolate what is not yet right. Marking an accepted answer not only rewards the volunteer who best helps you, it signifies to future researchers that the answer indeed solves the issue. – mickmackusa Apr 6 '18 at 2:04
1

You firstly need to encode your array in Javascript, like so:

var options = JSON.stringify(['foo', 'bar']);

$.post('index.php?option=component&view=componentview&Itemid=123&tmpl=component&layout=xlsx&options=' + options);

Then in your PHP code, use the following instead of $_POST:

$input = JFactory::getApplication()->input;
$xlsx  = json_decode($input->get('xlsx_options'));

echo $xlsx->foo;
echo $xlsx->bar;
  • Is it recommended to specify ->post() for this question? – mickmackusa Apr 6 '18 at 2:00

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.