23

I'm trying to send an array to another page.

What I tryed before was:

page1

<input type='hidden' name='input_name' value='<?php print_r($array_name); ?>' />

And page2

<?php 
$passed_array = $_POST['input_name'];
?>

Now how do I make $passed_array act like an array?

Or do you know of any other way of solving this problem?

Thanks, Mike.

Edit: The reason I want to do it this way is because I need to avoid sessions and cookies.

8 Answers 8

51

You could put it in the session:

session_start();
$_SESSION['array_name'] = $array_name;

Or if you want to send it via a form you can serialize it:

<input type='hidden' name='input_name' value="<?php echo htmlentities(serialize($array_name)); ?>" />

$passed_array = unserialize($_POST['input_name']);

The session has the advantage that the client doesn't see it (therefore can't tamper with it) and it's faster if the array is large. The disadvantage is it could get confused if the user has multiple tabs open.

Edit: a lot of answers are suggesting using name="input_name[]". This won't work in the general case - it would need to be modified to support associative arrays, and modified a lot to support multidimensional arrays (icky!). Much better to stick to serialize.

Sign up to request clarification or add additional context in comments.

5 Comments

You can sign/encrypt the serialized array to prevent tampering.
Or insert a special unique value and check its presence upon receipt
Tryed the serilaize method and it ruins the html code for some reason .. =/
fixed: I wrote <input value="" /> insted of <input value='' /> .. this is answer is the way to go :-)
serialize puts a " on every array element. So write your html with a '. Just like <input type='hidden' name='arr' value='<?php echo serialize($array); ?>' />
5

I ran into some issues with the above examples when some values in my array contained line breaks. Some of my values also had characters from foreign languages which htmlentities kept screwing up. The following was my solution.

In the page that you want to pass the array from...

<INPUT TYPE="hidden" NAME="input_name" VALUE="<?= base64_encode(serialize($array_name)); ?>">

In the page that receives the array...

$array = unserialize(base64_decode($_POST["input_name"]));

2 Comments

This was very helpful, thank you. Slight typo (missing echo), should read: <input type='hidden' name='input_name' value="<?php echo base64_encode(serialize($array_name)); ?>" />
@Daniel <?= is the same as <?php echo
4

You could serialize the array, which turns it into a string, and then unserialize it afterwards, which turns it back into an array. Like this:

<input type='hidden' name='input_name' value='<?php serialize($array_name); ?>' />

and on page 2:

<?php $passed_array = unserialize($_POST['input_name']); ?>

Comments

2

You can not send the array all at once, you will have to either send each value individually:

<input type='hidden' name='input_name[]' value='<?php print_r($array_name[0]); ?>' />
<input type='hidden' name='input_name[]' value='<?php print_r($array_name[1]); ?>' />
<input type='hidden' name='input_name[]' value='<?php print_r($array_name[2]); ?>' />
...

Or look into json or serialization.

Comments

2

You can simply json_encode() the array then pass it as a string in the POST request. Used it many times. Works everytime like a young titty

1 Comment

make sure you json_decode it to get the array again.
1

Note that to work with serialized arrays, you need to use POST as the form's transmission method, as GET has a size limit somewhere around 1024 characters.

I'd use sessions wherever possible.

Comments

0

Change input_name to input_name[] in your input tag, then put an input tag for every value of the array.

http://phpprogramming.wordpress.com/2007/05/06/php-passing-array-using-hidden-form-element/

Comments

0
<?php
/*arraytransfer.php*/
echo "Array transfer<br>";
$name = array( "mike", "tom" );
$arrCnt = sizeof( $name );
echo "arrCnt: $arrCnt<br>";

echo "<form action=\"arrayrcv2.php\" method=\"POST\">";
echo "<INPUT TYPE=\"HIDDEN\" NAME=\"arrCnt\" VALUE=\"$arrCnt\">";
for( $i=0; $i < $arrCnt; $i++ ) {
  echo "<INPUT TYPE=\"HIDDEN\" NAME=\"name\" VALUE=\"$name[$i]\"> ";
}
echo "<input type=\"submit\" name=\"submit\" value=\"Submit me!\" />";
echo "</form>";
?>


<?php
/*arrayrecv.php */
$arrCnt = $_POST["arrCnt"];
echo "Receiving data arrCnt = $arrCnt<br>";
$name = array();
for( $i = 0; $i < $arrCnt; $i++ ) {
  $var = $_POST["name"];
if( $var != "" ) array_push($name, $var );
}
print_r($name);
?>

1 Comment

Adding at least some explanation to your answer and not just code would be nice

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.