Tip: How to pass array through POST method

First use the function json_encode() to encode the array. Then use htmlspecialchars() to avoid errors caused by the quotes in json. Just like the following code:

<?php
  ...
  echo '<input type="hidden" name="array" value="'.htmlspecialchars(json_encode($array)).'">';
  ...
?>

To obtain the array do the inverse operation. First decode using htmlspecialchars_decode() and then use json_decode() to build the original array.

<?php
  ...
  $array = json_decode(htmlspecialchars_decode($_POST['$array']));
  ...
?>

More info: http://stackoverflow.com/questions/4237090/how-to-pass-array-through-hidden-field

Published

20 August 2013

Category

post

Tags

blog comments powered by Disqus