简体   繁体   English

如何从Joomla视图表单传递数据数组并获取Joomla Controller中的数据数组

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

In view/default.php : 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. 其中$this->episode是一组数据。

In controllers/report.php : 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. 我正在使用Joomla 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 JSON格式

Similar idea to the one above, just use the JSON format to store the array. 与上面的想法类似,只需使用JSON格式来存储数组。 Check json_encode and json_decode 检查json_encodejson_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'); 使用$requestData = JRequest::get('post'); ( post , get or data accordingly) in any controller you are using. (在您使用的任何控制器中postget或相应data )。 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. Joomla有一些奇怪的行为,从任务重定向到视图,这可能会给你留下你正在丢失数据的印象。

Using Joomla 3.1.1 使用Joomla 3.1.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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