Passing Array Using Html Form Hidden Element












57















I am trying to post an array in a hidden field and want to retrieve that array after submitting form in php.



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


But getting only array string after printting the posted value.
So how can i solve it?










share|improve this question


















  • 1





    You need to serialize it in some way. Check out the serialize and json_encode functions. I'd recommend going with the json_encode.

    – Gazler
    Jul 1 '11 at 11:14
















57















I am trying to post an array in a hidden field and want to retrieve that array after submitting form in php.



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


But getting only array string after printting the posted value.
So how can i solve it?










share|improve this question


















  • 1





    You need to serialize it in some way. Check out the serialize and json_encode functions. I'd recommend going with the json_encode.

    – Gazler
    Jul 1 '11 at 11:14














57












57








57


18






I am trying to post an array in a hidden field and want to retrieve that array after submitting form in php.



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


But getting only array string after printting the posted value.
So how can i solve it?










share|improve this question














I am trying to post an array in a hidden field and want to retrieve that array after submitting form in php.



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


But getting only array string after printting the posted value.
So how can i solve it?







php






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jul 1 '11 at 11:13









ShrishailShrishail

423147




423147








  • 1





    You need to serialize it in some way. Check out the serialize and json_encode functions. I'd recommend going with the json_encode.

    – Gazler
    Jul 1 '11 at 11:14














  • 1





    You need to serialize it in some way. Check out the serialize and json_encode functions. I'd recommend going with the json_encode.

    – Gazler
    Jul 1 '11 at 11:14








1




1





You need to serialize it in some way. Check out the serialize and json_encode functions. I'd recommend going with the json_encode.

– Gazler
Jul 1 '11 at 11:14





You need to serialize it in some way. Check out the serialize and json_encode functions. I'd recommend going with the json_encode.

– Gazler
Jul 1 '11 at 11:14












8 Answers
8






active

oldest

votes


















94














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


and you will got $_POST['result'] as array



print_r($_POST['result']);






share|improve this answer
























  • this solution is very helpful for me thanks

    – Sourabh Sharma
    Oct 13 '18 at 16:04



















25














There are mainly two possible ways to achieve this:





  1. 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.



    Alternativley 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-8bit 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 serverside 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.




  2. 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 using result[$key] as name of each field.




Every of this 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.



An other 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






share|improve this answer


























  • I had to use single quotes around value attribute. Otherwise the data was being trimmed at the first double quote in the serialized version.

    – VeeK
    Sep 23 '17 at 10:29











  • I 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
    Jan 4 '18 at 0:02



















13














You can use serialize and base64_encode from client side,
After then use unserialize and base64_decode to server side
Like as:



In 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; ?>">


In Server side use:



    $postvalue = unserialize(base64_decode($_POST['result'])); 
print_r($postvalue) //your desired array data will be printed here





share|improve this answer
























  • This should be the correct answer, better than use foreach, you can pass objects into arrays, and multilevel arrays

    – Leonardo Sapuy
    Jun 23 '17 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
    Oct 12 '17 at 6:28













  • I agree, this should be the correct answer

    – barakadam
    Jan 19 '18 at 21:29



















6














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'])






share|improve this answer


























  • serialize can mess up your quotes or brackets and mix up with html.. I would use accepted answer

    – zachu
    May 8 '15 at 14:32



















1














it's better to encode first to JSON-string and then encode with base64 eg. on server side in reverse order: use first base64_decode then json_decode functions. so you will restore your php array






share|improve this answer































    1














    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






    share|improve this answer































      1














      <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






      share|improve this answer































        0














        you can do it like this



        <input type="hidden" name="result" value="<?php foreach($postvalue as $value) echo $postvalue.","; ?>">





        share|improve this answer



















        • 5





          Or just use implode ;-)

          – Alex
          Jul 1 '11 at 11:20











        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6547209%2fpassing-array-using-html-form-hidden-element%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        8 Answers
        8






        active

        oldest

        votes








        8 Answers
        8






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        94














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


        and you will got $_POST['result'] as array



        print_r($_POST['result']);






        share|improve this answer
























        • this solution is very helpful for me thanks

          – Sourabh Sharma
          Oct 13 '18 at 16:04
















        94














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


        and you will got $_POST['result'] as array



        print_r($_POST['result']);






        share|improve this answer
























        • this solution is very helpful for me thanks

          – Sourabh Sharma
          Oct 13 '18 at 16:04














        94












        94








        94







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


        and you will got $_POST['result'] as array



        print_r($_POST['result']);






        share|improve this answer













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


        and you will got $_POST['result'] as array



        print_r($_POST['result']);







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 1 '11 at 11:15









        Shakti SinghShakti Singh

        65.2k16115141




        65.2k16115141













        • this solution is very helpful for me thanks

          – Sourabh Sharma
          Oct 13 '18 at 16:04



















        • this solution is very helpful for me thanks

          – Sourabh Sharma
          Oct 13 '18 at 16:04

















        this solution is very helpful for me thanks

        – Sourabh Sharma
        Oct 13 '18 at 16:04





        this solution is very helpful for me thanks

        – Sourabh Sharma
        Oct 13 '18 at 16:04













        25














        There are mainly two possible ways to achieve this:





        1. 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.



          Alternativley 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-8bit 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 serverside 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.




        2. 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 using result[$key] as name of each field.




        Every of this 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.



        An other 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






        share|improve this answer


























        • I had to use single quotes around value attribute. Otherwise the data was being trimmed at the first double quote in the serialized version.

          – VeeK
          Sep 23 '17 at 10:29











        • I 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
          Jan 4 '18 at 0:02
















        25














        There are mainly two possible ways to achieve this:





        1. 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.



          Alternativley 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-8bit 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 serverside 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.




        2. 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 using result[$key] as name of each field.




        Every of this 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.



        An other 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






        share|improve this answer


























        • I had to use single quotes around value attribute. Otherwise the data was being trimmed at the first double quote in the serialized version.

          – VeeK
          Sep 23 '17 at 10:29











        • I 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
          Jan 4 '18 at 0:02














        25












        25








        25







        There are mainly two possible ways to achieve this:





        1. 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.



          Alternativley 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-8bit 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 serverside 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.




        2. 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 using result[$key] as name of each field.




        Every of this 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.



        An other 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






        share|improve this answer















        There are mainly two possible ways to achieve this:





        1. 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.



          Alternativley 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-8bit 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 serverside 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.




        2. 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 using result[$key] as name of each field.




        Every of this 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.



        An other 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







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jun 10 '14 at 18:11









        Plyto

        5411818




        5411818










        answered Jul 1 '11 at 11:49









        s1lences1lence

        1,68621331




        1,68621331













        • I had to use single quotes around value attribute. Otherwise the data was being trimmed at the first double quote in the serialized version.

          – VeeK
          Sep 23 '17 at 10:29











        • I 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
          Jan 4 '18 at 0:02



















        • I had to use single quotes around value attribute. Otherwise the data was being trimmed at the first double quote in the serialized version.

          – VeeK
          Sep 23 '17 at 10:29











        • I 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
          Jan 4 '18 at 0:02

















        I had to use single quotes around value attribute. Otherwise the data was being trimmed at the first double quote in the serialized version.

        – VeeK
        Sep 23 '17 at 10:29





        I had to use single quotes around value attribute. Otherwise the data was being trimmed at the first double quote in the serialized version.

        – VeeK
        Sep 23 '17 at 10:29













        I 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
        Jan 4 '18 at 0:02





        I 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
        Jan 4 '18 at 0:02











        13














        You can use serialize and base64_encode from client side,
        After then use unserialize and base64_decode to server side
        Like as:



        In 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; ?>">


        In Server side use:



            $postvalue = unserialize(base64_decode($_POST['result'])); 
        print_r($postvalue) //your desired array data will be printed here





        share|improve this answer
























        • This should be the correct answer, better than use foreach, you can pass objects into arrays, and multilevel arrays

          – Leonardo Sapuy
          Jun 23 '17 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
          Oct 12 '17 at 6:28













        • I agree, this should be the correct answer

          – barakadam
          Jan 19 '18 at 21:29
















        13














        You can use serialize and base64_encode from client side,
        After then use unserialize and base64_decode to server side
        Like as:



        In 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; ?>">


        In Server side use:



            $postvalue = unserialize(base64_decode($_POST['result'])); 
        print_r($postvalue) //your desired array data will be printed here





        share|improve this answer
























        • This should be the correct answer, better than use foreach, you can pass objects into arrays, and multilevel arrays

          – Leonardo Sapuy
          Jun 23 '17 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
          Oct 12 '17 at 6:28













        • I agree, this should be the correct answer

          – barakadam
          Jan 19 '18 at 21:29














        13












        13








        13







        You can use serialize and base64_encode from client side,
        After then use unserialize and base64_decode to server side
        Like as:



        In 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; ?>">


        In Server side use:



            $postvalue = unserialize(base64_decode($_POST['result'])); 
        print_r($postvalue) //your desired array data will be printed here





        share|improve this answer













        You can use serialize and base64_encode from client side,
        After then use unserialize and base64_decode to server side
        Like as:



        In 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; ?>">


        In Server side use:



            $postvalue = unserialize(base64_decode($_POST['result'])); 
        print_r($postvalue) //your desired array data will be printed here






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered May 28 '16 at 11:35









        Kabir HossainKabir Hossain

        1,2321123




        1,2321123













        • This should be the correct answer, better than use foreach, you can pass objects into arrays, and multilevel arrays

          – Leonardo Sapuy
          Jun 23 '17 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
          Oct 12 '17 at 6:28













        • I agree, this should be the correct answer

          – barakadam
          Jan 19 '18 at 21:29



















        • This should be the correct answer, better than use foreach, you can pass objects into arrays, and multilevel arrays

          – Leonardo Sapuy
          Jun 23 '17 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
          Oct 12 '17 at 6:28













        • I agree, this should be the correct answer

          – barakadam
          Jan 19 '18 at 21:29

















        This should be the correct answer, better than use foreach, you can pass objects into arrays, and multilevel arrays

        – Leonardo Sapuy
        Jun 23 '17 at 16:32





        This should be the correct answer, better than use foreach, you can pass objects into arrays, and multilevel arrays

        – Leonardo Sapuy
        Jun 23 '17 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
        Oct 12 '17 at 6:28







        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
        Oct 12 '17 at 6:28















        I agree, this should be the correct answer

        – barakadam
        Jan 19 '18 at 21:29





        I agree, this should be the correct answer

        – barakadam
        Jan 19 '18 at 21:29











        6














        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'])






        share|improve this answer


























        • serialize can mess up your quotes or brackets and mix up with html.. I would use accepted answer

          – zachu
          May 8 '15 at 14:32
















        6














        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'])






        share|improve this answer


























        • serialize can mess up your quotes or brackets and mix up with html.. I would use accepted answer

          – zachu
          May 8 '15 at 14:32














        6












        6








        6







        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'])






        share|improve this answer















        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'])







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Aug 9 '12 at 20:40







        user212218

















        answered Jul 1 '11 at 11:21









        Ēriks DalibaĒriks Daliba

        65844




        65844













        • serialize can mess up your quotes or brackets and mix up with html.. I would use accepted answer

          – zachu
          May 8 '15 at 14:32



















        • serialize can mess up your quotes or brackets and mix up with html.. I would use accepted answer

          – zachu
          May 8 '15 at 14:32

















        serialize can mess up your quotes or brackets and mix up with html.. I would use accepted answer

        – zachu
        May 8 '15 at 14:32





        serialize can mess up your quotes or brackets and mix up with html.. I would use accepted answer

        – zachu
        May 8 '15 at 14:32











        1














        it's better to encode first to JSON-string and then encode with base64 eg. on server side in reverse order: use first base64_decode then json_decode functions. so you will restore your php array






        share|improve this answer




























          1














          it's better to encode first to JSON-string and then encode with base64 eg. on server side in reverse order: use first base64_decode then json_decode functions. so you will restore your php array






          share|improve this answer


























            1












            1








            1







            it's better to encode first to JSON-string and then encode with base64 eg. on server side in reverse order: use first base64_decode then json_decode functions. so you will restore your php array






            share|improve this answer













            it's better to encode first to JSON-string and then encode with base64 eg. on server side in reverse order: use first base64_decode then json_decode functions. so you will restore your php array







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jul 1 '11 at 11:15









            heximalheximal

            8,82843560




            8,82843560























                1














                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






                share|improve this answer




























                  1














                  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






                  share|improve this answer


























                    1












                    1








                    1







                    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






                    share|improve this answer













                    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







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jul 1 '11 at 11:15









                    Nicola PeluchettiNicola Peluchetti

                    62.4k24117166




                    62.4k24117166























                        1














                        <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






                        share|improve this answer




























                          1














                          <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






                          share|improve this answer


























                            1












                            1








                            1







                            <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






                            share|improve this answer













                            <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







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Aug 19 '15 at 15:41









                            Diagboya EwereDiagboya Ewere

                            565




                            565























                                0














                                you can do it like this



                                <input type="hidden" name="result" value="<?php foreach($postvalue as $value) echo $postvalue.","; ?>">





                                share|improve this answer



















                                • 5





                                  Or just use implode ;-)

                                  – Alex
                                  Jul 1 '11 at 11:20
















                                0














                                you can do it like this



                                <input type="hidden" name="result" value="<?php foreach($postvalue as $value) echo $postvalue.","; ?>">





                                share|improve this answer



















                                • 5





                                  Or just use implode ;-)

                                  – Alex
                                  Jul 1 '11 at 11:20














                                0












                                0








                                0







                                you can do it like this



                                <input type="hidden" name="result" value="<?php foreach($postvalue as $value) echo $postvalue.","; ?>">





                                share|improve this answer













                                you can do it like this



                                <input type="hidden" name="result" value="<?php foreach($postvalue as $value) echo $postvalue.","; ?>">






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Jul 1 '11 at 11:14









                                genesisgenesis

                                43.5k1582111




                                43.5k1582111








                                • 5





                                  Or just use implode ;-)

                                  – Alex
                                  Jul 1 '11 at 11:20














                                • 5





                                  Or just use implode ;-)

                                  – Alex
                                  Jul 1 '11 at 11:20








                                5




                                5





                                Or just use implode ;-)

                                – Alex
                                Jul 1 '11 at 11:20





                                Or just use implode ;-)

                                – Alex
                                Jul 1 '11 at 11:20


















                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6547209%2fpassing-array-using-html-form-hidden-element%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Costa Masnaga

                                Fotorealismo

                                Sidney Franklin