我在PHP中有一个名为$rebate_by_product
的数组,我已经将这个数组分配给一个smarty模板,如下所示:
$smarty->assign('preview_data', $rebate_by_product);
数组$rebate_by_product
的内容如下:
Array
(
[op] => preview
[id] =>
[form_submitted] => yes
[company_id] => 46
[1] => Array
(
[pack] => 10
[quantity] => 20
[volume] => 30
[units] => 5
[amount] => 40
[rebate_start_date] => 2014-05-01
[rebate_expiry_date] => 2014-05-08
[applicable_states] => Array
(
[0] => 2
[1] => 6
[2] => 14
)
[rebate_total_count] => 5000
[products] => Array
(
[1] => 10
)
)
[2] => Array
(
[pack] => 50
[quantity] => 60
[volume] => 70
[units] => 12
[amount] => 80
[rebate_start_date] => 2014-05-09
[rebate_expiry_date] => 2014-05-15
[applicable_states] => Array
(
[0] => 7
[1] => 12
[2] => 17
)
[rebate_total_count] => 10000
[products] => Array
(
[1] => 9
)
)
[3] => Array
(
[pack] => 500
[quantity] => 1000
[volume] => 1500
[units] => 9
[amount] => 2000
[rebate_start_date] => 2014-05-21
[rebate_expiry_date] => 2014-05-31
[applicable_states] => Array
(
[0] => 46
[1] => 48
[2] => 50
)
[rebate_total_count] => 9000
[products] => Array
(
[1] => 11
)
)
[multiselect] => 50
)
现在我想将整个数组存储到smarty模板中表单的隐藏字段中。当用户提交这个,我应该得到整个数组在$_POST
。如何做到这一点?我试着用下面的代码,但它没有工作:
<input type="hidden" class="form-control" name="reb_data" id="reb_data" value="{$preview_data}">
提交表单后,如果我打印$_POST
,我会得到以下输出。
Array
(
[reb_data] => Array
)
实际上我希望整个数组为$_POST
中的$rebate_by_product
,键为[reb_data]
您可以使用serialze()
/unserialize()
完全用于此目的。赋值会变成这样:
$smarty->assign('preview_data', serialize($rebate_by_product));
在下一页,您可以像这样获得数组:
$rebate_by_product = unserialize($_POST['reb_data']);
如果不成功,函数将返回FALSE
。
不能发送数组作为输入。你需要做的是改变格式:
$smarty->assign('preview_data', htmlentities (json_encode($rebate_by_product),ENT_QUOTES));
发送表单后,你需要在PHP中输入:
$rebate_by_product = json_decode($_POST['reb_data']);
您还需要考虑如何处理这些数据。你必须假设用户可以改变它,所以你不能确定你收到的数据会是相同的,你分配给他们之前显示的形式(你可以在网站功能干扰或黑客-他们是真实的阅读这些问题)。