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

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

问题描述

在这里我的code

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

隐藏字段 -

hidden field-

<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 不是。 打印($ _ POST ['hdnOrder'])只打印阵列在屏幕上。

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.'">';

当这个值回来,你需要解序列化它让你的阵列回来。虽然容易,我不建议这个除非你有一些额外的机制,以prevent篡改,如安全散列,否则任何人都可以注入任何他们喜欢的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 EN code / 德code 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天全站免登陆