Passing an array using a hidden element in HTML form

I am trying to place an array in a hidden field and want to get this array after submitting the form in PHP.

$postvalue = array("a", "b", "c"); <input type="hidden" name="result" value="<?php echo $postvalue; ?>"> 

But I only get the array string after printing the sent value. So how can I solve this?

+61
php
Jul 01 '11 at 11:13
source share
8 answers

Using:

 $postvalue = array("a", "b", "c"); foreach($postvalue as $value) { echo '<input type="hidden" name="result[]" value="'. $value. '">'; } 

And you get $_POST['result'] as an array.

 print_r($_POST['result']); 
+102
Jul 01 '11 at 11:15
source share

There are basically two possible ways to achieve this:

  1. Serializing data in some way:

     $postvalue = serialize($array); // Client side $array = unserialize($_POST['result']; // Server side 

And then you can deserialize the published values ​​with unserialize($postvalue) . Further information on this is here in the PHP manuals .

Alternatively, you can use the json_encode() and json_decode() functions to get a serialized string in JSON format. You could even reduce the transmitted data with gzcompress() (note that this is gzcompress() performance) and protect the transmitted data with base64_encode() (so that your data survives in non-8-bit pure transport layers). It might look like this:

  $postvalue = base64_encode(json_encode($array)); // Client side $array = json_decode(base64_decode($_POST['result'])); // Server side 

The not recommended way to serialize your data (but very cheap in terms of performance) is to simply use implode() in your array to get a string with all values ​​separated by any given character. On the server side, you can get the array using explode() . But note that you should not use a character to separate that occurs in the values ​​of the array (or then escape it), and that you cannot pass the keys of the array using this method.

  1. Use the properties of special named input elements:

     $postvalue = ""; foreach ($array as $v) { $postvalue .= '<input type="hidden" name="result[]" value="' .$v. '" />'; } 

    Thus, you will receive the entire array in the variable $_POST['result'] if the form is submitted. Note that this does not pass array keys. However, you can achieve this by using result[$key] as the name of each field.

Each of these methods has its advantages and disadvantages. What you use mostly depends on how big your array will be, since you should try to send a minimal amount of data with all of these methods.

Another way to achieve the same thing is to save the array in a session on the server side, rather than passing it on the client side. Thus, you can access the array through the $_SESSION variable and do not have to pass anything in form. To do this, take a look at a basic example of using sessions on php.net .

+29
Jul 01 2018-11-11T00:
source share

You can use serialize and base64_encode on the client side. After that, use unserialize and base64_decode on the server side.

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 
+16
May 28 '16 at 11:35 a.m.
source share

Or serialize:

 $postvalue=array("a","b","c"); <input type="hidden" name="result" value="<?php echo serialize($postvalue); ?>"> 

upon receipt: unserialize($_POST['result'])

Or explode:

 $postvalue=array("a","b","c"); <input type="hidden" name="result" value="<?php echo implode(',', $postvalue); ?>"> 

On receipt: explode(',', $_POST['result'])

+6
Jul 01 '11 at 11:21
source share

It is better to first encode into a JSON string, and then encode using Base64, for example, on the server side in the reverse order: first use the base64_decode functions, and then json_decode. This way you will restore your PHP array.

+1
Jul 01 '11 at 11:15
source share

If you want to publish an array, you must use a different notation:

 foreach ($postvalue as $value){ <input type="hidden" name="result[]" value="$value."> } 

this way you have three input fields with the result of the name [], and when you publish $_POST['result'] will be an array

+1
Jul 01 '11 at 11:15
source share
 <input type="hidden" name="item[]" value="[anyvalue]"> 

Let it be in repeated mode, it will output this element in the form as an array and use

 print_r($_POST['item']) 

To get an item

+1
Aug 19 '15 at 15:41
source share

You can do it like this:

 <input type="hidden" name="result" value="<?php foreach($postvalue as $value) echo $postvalue.","; ?>"> 
-one
Jul 01 '11 at 11:14
source share



All Articles