I am trying to post an array in a hidden field and want to retrieve that array after submitting a form in 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?
-
1You need to serialize it in some way. Check out the serialize and json_encode functions. I'd recommend going with the json_encode.Gazler– Gazler2011年07月01日 11:14:51 +00:00Commented Jul 1, 2011 at 11:14
8 Answers 8
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.
print_r($_POST['result']);
-
so for an array of arrays, will the name look like this name="result[[]]" ?Samuel Giftson– Samuel Giftson2019年11月18日 16:33:00 +00:00Commented Nov 18, 2019 at 16:33
-
2@SamuelGiftson maybe like this
name="result[][]"
Nurkartiko– Nurkartiko2020年06月04日 15:50:23 +00:00Commented Jun 4, 2020 at 15:50
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
And then you can unserialize the posted values with unserialize($postvalue)
. Further information on this is here in the PHP manuals.
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:
$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.
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.
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.
-
1I had to use single quotes around
value
attribute. Otherwise the data was being trimmed at the first double quote in the serialized version.Whip– Whip2017年09月23日 10:29:55 +00:00Commented Sep 23, 2017 at 10:29 -
1I did this using json_encode. The second argument for json_decode had to be true in my case (to return associative array instead of StdClass as the default)jamil– jamil2018年01月04日 00:02:09 +00:00Commented Jan 4, 2018 at 0:02
You can use serialize and base64_encode from the client side. After that, then use unserialize and base64_decode on the server side.
Like:
On the client side, use:
$postvalue = array("a", "b", "c");
$postvalue = base64_encode(serialize($postvalue));
// 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
-
This should be the correct answer, better than use foreach, you can pass objects into arrays, and multilevel arraysLeonardo Sapuy– Leonardo Sapuy2017年06月23日 16:32:56 +00:00Commented Jun 23, 2017 at 16:32
-
This answer doesn't get enough credit. Just using serialize() or json_encode() are things that most people have tried and realize fail on the basis of quotation marks by the time they get here. This answer takes it a step further and encodes it in a way that makes that issue irrelevant in an elegant manner. Bravo!Kaji– Kaji2017年10月12日 06:28:41 +00:00Commented Oct 12, 2017 at 6:28
-
thank you this is exactly what I was looking for. One small remark. $postvalue = base64_encode(serialize($array)); should be $postvalue = base64_encode(serialize($postvalue));anatak– anatak2020年05月08日 02:55:23 +00:00Commented May 8, 2020 at 2:55
Either serialize:
$postvalue=array("a","b","c");
<input type="hidden" name="result" value="<?php echo serialize($postvalue); ?>">
on receive: 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'])
-
2serialize can mess up your quotes or brackets and mix up with html.. I would use accepted answerzachu– zachu2015年05月08日 14:32:58 +00:00Commented May 8, 2015 at 14:32
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
<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
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.
You can do it like this:
<input type="hidden" name="result" value="<?php foreach($postvalue as $value) echo $postvalue.","; ?>">