如何通过隐藏字段传递数组 [英] how to pass array through hidden field

查看:20
本文介绍了如何通过隐藏字段传递数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我的代码

$order[$j][0]="Euclidean Geomethiyil Kodpagugal";
$order[$j][1]=$q16;
$j++;

隐藏字段-

<input type="hidden" name="hdnTotal" value="<?php echo $gtot; ?>">
<input type="hidden" name="hdnOrder" value="<?php echo $order; ?>">
<input type="submit" value="Place Order">

hdnTotal 值出现在下一页,但 hdnOrder 不是.print($_POST['hdnOrder']) 只在屏幕上打印 Array.

hdnTotal value is coming in next page but hdnOrder is not. print($_POST['hdnOrder']) print only Array on screen.

推荐答案

您可以序列化数组,也可以使用大量隐藏字段.或者,将其存储在会话中.

You can either serialize the array, or use lots of hidden fields. Alternatively, store this in a session.

序列化,您将只使用一个隐藏字段.如果您的数组包含非标量数据,这是一种有用的技术.

To serialize, you'll use just one hidden field. This is a useful technique if your array contains non-scalar data.

 $data=serialize($order); 
 $encoded=htmlentities($data);
 echo '<input type="hidden" name="order" value="'.$encoded.'">';

当此值返回时,您需要反序列化以将数组取出.虽然很简单,但我不会推荐这个,除非你有一些额外的机制来防止篡改,比如安全哈希,否则任何人都可以注入他们喜欢的任何 PHP 数据结构!

When this value comes back, you need to unserialize it to get your array back out. While easy, I wouldn't recommend this unless you have some additional mechanism to prevent tampering, like a security hash, otherwise anyone can inject any PHP data structure they like!

哈希可能是这样完成的:

A hash might be done like this:

 $data=serialize($order); 
 $encoded=htmlentities($data);
 $hash=md5($encoded.'SecretStringHere');
 echo '<input type="hidden" name="order" value="'.$encoded.'">';
 echo '<input type="hidden" name="order_hash" value="'.$hash.'">';

现在,当数据返回时,在反序列化之前,您再次生成散列并检查它是否与表单中的散列值匹配.如果不匹配,则有人篡改了数据.如果它匹配,那么您知道生成的数据也知道您的秘密字符串.这应该只是你!

Now, when the data comes back, before you unserialize, you generate the hash again and check it matches the hash value from the form. If it doesn't match, someone tampered with the data. If it does match, then you know that whatever generated the data also knows your secret string. Which should be just you!

最后,如果 Javascript 理解数组数据有用,那么使用 JSON encode/解码 PHP 函数会更合适.

Finally, if it will be useful for Javascript to understand the array data, then using JSON encode/decode functions of PHP would be more appropriate.

假设一个由标量值组成的简单数组,您可以使用很多隐藏字段

Assuming a simple array consisting of scalar values, you can use lots of hidden fields

 foreach($order as $idx=>$value)
 {
      $name=htmlentities('order['.$idx.']');
      $value=htmlentities($val);
      echo '<input type="hidden" name="'.$name.'" value="'.$value.'">';

 }

这样做的好处是 PHP 会自动将其重新创建为数组 给你.

This has the advantage that PHP will automatically recreate this as an array for you.

因为您的数组是二维的,所以要使用此技术,您将需要一个用于第二维的内部循环.读者练习....

Because your array is 2 dimensional, to use this technique you'll need an inner loop for the second dimension. An exercise for the reader....

也许是三个中最简单的....

Perhaps the easiest of the three....

session_start();

$_SESSION['order']=$order;

一旦设置,数组在您调用 session_start() 后可用.这样做的好处是它永远不会离开服务器,但在一段时间不活动后当然会消失(默认为 24​​ 分钟)

Once set, the array is available after you've called session_start(). This has the advantage that it never leaves the server, but will of course disappear after a period of inactivity (24 minutes is the default)

这篇关于如何通过隐藏字段传递数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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