简体   繁体   中英

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.

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

Try this

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM