Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将分配给smarty模板的关联数组存储到隐藏字段中,并在表单提交时将其返回PHP?_Php_Arrays_Forms_Post_Hidden Field - Fatal编程技术网

如何将分配给smarty模板的关联数组存储到隐藏字段中,并在表单提交时将其返回PHP?

如何将分配给smarty模板的关联数组存储到隐藏字段中,并在表单提交时将其返回PHP?,php,arrays,forms,post,hidden-field,Php,Arrays,Forms,Post,Hidden Field,我在PHP中有一个名为$retrieve\u by\u product的数组,我已将此数组分配给smarty模板,如下所示: $smarty->assign('preview_data', $rebate_by_product); 以下是数组$retriep\u by\u product的内容: Array ( [op] => preview [id] => [form_submitted] => yes

我在PHP中有一个名为
$retrieve\u by\u product
的数组,我已将此数组分配给smarty模板,如下所示:

$smarty->assign('preview_data', $rebate_by_product);
以下是数组
$retriep\u by\u 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模板中表单上的隐藏字段中。当用户提交此文件时,我应该在
$\u POST
中获取整个数组。如何做到这一点?我尝试使用以下代码,但无效:

<input type="hidden" class="form-control" name="reb_data" id="reb_data" value="{$preview_data}">
实际上,我希望整个数组在
$\u POST
[reb\u data]
下的
$retrieve\u by\u product
中作为
$retrieve\u by\u product
,您可以/正好用于此目的。分配将变成这样:

$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']);

您还需要考虑如何处理这些数据。您必须假设用户可以对其进行更改,这样您就无法确保您收到的数据与您在显示表单之前分配给他们的数据相同(您可以在中了解这些问题-它们是真实的)。

它还不起作用。当我提交表格时,会出现空白页。这出了什么问题?它还没起作用。当我提交表格时,会出现空白页。这出了什么问题?我在上面的代码中添加了htmlentities。现在发送应该可以正常工作,但是,您决定发送数据(序列化等)并稍后重新加载。我通常在发送之前对其进行base64_编码,在取回时进行base64_解码。通过这种方式,它肯定不会被HTML处理改变。
$rebate_by_product = json_decode($_POST['reb_data']);