php 使用 HTML 表单隐藏元素传递数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6547209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing an array using an HTML form hidden element
提问by Shrishail
I am trying to post an array in a hidden field and want to retrieve that array after submitting a form in PHP.
我试图在隐藏字段中发布一个数组,并希望在用 PHP 提交表单后检索该数组。
$postvalue = array("a", "b", "c");
<input type="hidden" name="result" value="<?php echo $postvalue; ?>">
But I am getting only an array string after printing the posted value. So how can I solve it?
但是在打印发布的值后我只得到一个数组字符串。那么我该如何解决呢?
回答by Shakti Singh
Use:
用:
$postvalue = array("a", "b", "c");
foreach($postvalue as $value)
{
echo '<input type="hidden" name="result[]" value="'. $value. '">';
}
And you will get $_POST['result']
as an array.
你会得到$_POST['result']
一个数组。
print_r($_POST['result']);
回答by s1lence
There are mainly two possible ways to achieve this:
主要有两种可能的方法来实现这一点:
Serialize the data in some way:
$postvalue = serialize($array); // Client side $array = unserialize($_POST['result']; // Server side
以某种方式序列化数据:
$postvalue = serialize($array); // Client side $array = unserialize($_POST['result']; // Server side
And then you can unserialize the posted values with unserialize($postvalue)
. Further information on this is here in the PHP manuals.
然后您可以使用unserialize($postvalue)
. 有关这方面的更多信息,请参见 PHP 手册。
Alternativeley you can use the json_encode()
and json_decode()
functions to get a JSON formatted serialized string. You could even shrink the transmitted data with gzcompress()
(note that this is performance intensive) and secure the transmitted data with base64_encode()
(to make your data survive in non-8 bit clean transport layers) This could look like this:
或者,您可以使用json_encode()
和json_decode()
函数来获取 JSON 格式的序列化字符串。您甚至可以缩小传输的数据gzcompress()
(请注意,这是性能密集型的)并保护传输的数据base64_encode()
(使您的数据在非 8 位干净的传输层中存活)这可能如下所示:
$postvalue = base64_encode(json_encode($array)); // Client side
$array = json_decode(base64_decode($_POST['result'])); // Server side
A not recommended way to serialize your data (but very cheap in performance) is to simply use implode()
on your array to get a string with all values separated by some specified character. On the server side you can retrieve the array with explode()
then. But note that you shouldn't use a character for separation that occurs in the array values (or then escape it) and that you cannot transmit the array keys with this method.
一种不推荐的序列化数据的方法(但性能非常便宜)是简单地implode()
在数组上使用以获取所有值由某个指定字符分隔的字符串。在服务器端,您可以使用explode()
then检索数组。但请注意,您不应该使用出现在数组值中的分隔字符(或转义它),并且您不能使用此方法传输数组键。
Use the properties of special named input elements:
$postvalue = ""; foreach ($array as $v) { $postvalue .= '<input type="hidden" name="result[]" value="' .$v. '" />'; }
Like this you get your entire array in the
$_POST['result']
variable if the form is sent. Note that this doesn't transmit array keys. However you can achieve this by usingresult[$key]
as name of each field.
使用特殊命名输入元素的属性:
$postvalue = ""; foreach ($array as $v) { $postvalue .= '<input type="hidden" name="result[]" value="' .$v. '" />'; }
像这样,
$_POST['result']
如果发送表单,您将在变量中获得整个数组。请注意,这不会传输数组键。但是,您可以通过使用result[$key]
作为每个字段的名称来实现这一点。
Everyone of these methods got their own advantages and disadvantages. What you use is mainly depending on how large your array will be, since you should try to send a minimal amount of data with all of this methods.
这些方法中的每一种都有自己的优点和缺点。您使用什么主要取决于您的阵列有多大,因为您应该尝试使用所有这些方法发送最少量的数据。
Another way to achieve the same is to store the array in a server side session instead of transmitting it client side. Like this you can access the array over the $_SESSION
variable and don't have to transmit anything over the form. For this have a look at a basic usage example of sessions on php.net.
另一种实现相同目的的方法是将数组存储在服务器端会话中,而不是在客户端传输它。像这样,您可以通过$_SESSION
变量访问数组,而不必通过表单传输任何内容。为此,请查看php.net 上会话的基本用法示例。
回答by Kabir Hossain
You can use serialize and base64_encode from the client side. After that, then use unserialize and base64_decode on the server side.
您可以从客户端使用 serialize 和 base64_encode。之后,然后在服务器端使用反序列化和base64_decode。
Like:
喜欢:
On the client side, use:
在客户端,使用:
$postvalue = array("a", "b", "c");
$postvalue = base64_encode(serialize($array));
// Your form hidden input
<input type="hidden" name="result" value="<?php echo $postvalue; ?>">
On the server side, use:
在服务器端,使用:
$postvalue = unserialize(base64_decode($_POST['result']));
print_r($postvalue) // Your desired array data will be printed here
回答by ēriks Daliba
Either serialize:
要么序列化:
$postvalue=array("a","b","c");
<input type="hidden" name="result" value="<?php echo serialize($postvalue); ?>">
on receive: unserialize($_POST['result'])
接收时: unserialize($_POST['result'])
Or implode:
或内爆:
$postvalue=array("a","b","c");
<input type="hidden" name="result" value="<?php echo implode(',', $postvalue); ?>">
On receive: explode(',', $_POST['result'])
接收时: explode(',', $_POST['result'])
回答by Diagboya Ewere
<input type="hidden" name="item[]" value="[anyvalue]">
Let it be in a repeated mode it will post this element in the form as an array and use the
让它处于重复模式,它将将此元素作为数组发布在表单中并使用
print_r($_POST['item'])
To retrieve the item
取回物品
回答by heximal
It's better to encode first to a JSON string and then encode with Base64, for example, on the server side in reverse order: use first the base64_decode and then json_decode functions. So you will restore your PHP array.
最好先编码为 JSON 字符串,然后使用 Base64 编码,例如,在服务器端以相反的顺序:先使用 base64_decode 再使用 json_decode 函数。所以你将恢复你的 PHP 数组。
回答by Nicola Peluchetti
If you want to post an array you must use another notation:
如果要发布数组,则必须使用另一种表示法:
foreach ($postvalue as $value){
<input type="hidden" name="result[]" value="$value.">
}
in this way you have three input fields with the name result[] and when posted $_POST['result']
will be an array
通过这种方式,您有三个名称为 result[] 的输入字段,发布时$_POST['result']
将是一个数组
回答by genesis
You can do it like this:
你可以这样做:
<input type="hidden" name="result" value="<?php foreach($postvalue as $value) echo $postvalue.","; ?>">