Passing an array using an HTML form hidden element

前端 未结 8 2001
再見小時候
再見小時候 2020-11-27 12:40

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\");


        
相关标签:
8条回答
  • 2020-11-27 13:40

    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']);
    
    0 讨论(0)
  • 2020-11-27 13:42

    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($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
    
    0 讨论(0)
提交回复
热议问题