如何存储分配给Smarty模板到隐藏字段关联数组,并把它找回来在PHP中的表单提交? [英] How to store an associative array assigned to smarty template into hidden field and get it back in PHP on form submission?

查看:109
本文介绍了如何存储分配给Smarty模板到隐藏字段关联数组,并把它找回来在PHP中的表单提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP题为数组 $ rebate_by_product 和我如下分配这个数组一个智者的模板:

I've an array titled $rebate_by_product in PHP and I've assigned this array to a smarty template as follows:

$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
    )

现在,我想这整个数组转换成一个隐藏字段存储智者模板的形式。当用户提交这个对我应该得到的 $ _ POST 整个数组。如何实现这一目标?我试着用以下code,但它没有工作:

Now I want to store this entire array into a hidden field on a form in smarty template. When user submits this for I should get the entire array in $_POST. How to achieve this? I tried with following code but it didn't work:

<input type="hidden" class="form-control" name="reb_data" id="reb_data" value="{$preview_data}">

在提交表单我得到下面的输出,如果我打印的 $ _ POST

After submission of form I'm getting following output if I print $_POST.

Array
(
[reb_data] => Array
)

其实我想下键整个数组为 $ rebate_by_product $ _ POST [ reb_data]

推荐答案

您无法将数组作为输入。你必须做什么改变格式为:

You cannot send arrays as input. What you have to do change format:

$smarty->assign('preview_data', htmlentities (json_encode($rebate_by_product),ENT_QUOTES));

和发送表单后,你必须用PHP做什么:

And after sending form you have to do in PHP:

$rebate_by_product = json_decode($_POST['reb_data']);

您还需要考虑你想要使用此数据做什么。你必须假设用户可以改变它,所以你不能确保数据您收到一定是你展示的形式分配给前,它们是相同的(你可以看到在这样的问题,<一个href=\"http://stackoverflow.com/questions/23471388/website-functionality-disturbed-or-hacked/23471689\">Website功能干扰或砍死 - 它们是真实的)

You also need to think what you want to do with this data. You have to assume that user could change it so you cannot be sure data you receive would be the same you assign them before displaying form (you can read about such issues in Website functionality disturbed or hacked - they are real).

这篇关于如何存储分配给Smarty模板到隐藏字段关联数组,并把它找回来在PHP中的表单提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆