nkdweb
nkdweb

Reputation: 13

How to pass array of data from Joomla view form and get the array of data in Joomla Controller

In view/default.php:

<form action="<?php echo JRoute::_('index.php?option=com_scheduler&view=report');?>" method="post" name="rform">
<input type="submit" class="button" value="Generate Report" />
<input type="hidden" name="task" value="report.generate" />
<input type="hidden" name="data" value="<?php echo $this->epsiode; ?>" />
</form>

Where $this->episode is an array of data.

In controllers/report.php:

function generate(){
    $items = JRequest::getVar('data',array(), 'post', 'array');
    print_r($items);
}

The output is

Array ( [0] => Array )

Please suggest me how to get the array data. I'm using Joomla version 2.5.x.

Upvotes: 0

Views: 4161

Answers (3)

alexismorin
alexismorin

Reputation: 856

Use $requestData = JRequest::get('post'); (post, get or data accordingly) in any controller you are using. That should provide you with all the data sent over from the form you are using. Make sure this is the controller that your form posts to. Joomla has some strange behaviour with redirecting from tasks to views, which might give the impression you are losing data somewhere along the way.

Using Joomla 3.1.1

Upvotes: 0

Jobin
Jobin

Reputation: 8282

Try this

<?php 
$content = $this->epsiode;
for($i=0;$i<sizeof($content);$i++) { ?>
<input type="hidden" name="data[]" value="<?php echo $content[$i] ; ?>" />
<?php } ?>

Upvotes: 1

Valentin Despa
Valentin Despa

Reputation: 42622

Not clear what the hidden field data contains, but if you are just echoing an array, the final result does not surprise me.

I suggest one of the options (multiple solutions possible).

Serializing the array

    <input type="hidden" name="data" value="<?php echo serialize(htmlentities($this->epsiode)); ?>" />

On the controller, make sure you unserialize the data.

JSON format

Similar idea to the one above, just use the JSON format to store the array. Check json_encode and json_decode

Upvotes: 1

Related Questions