Problems passing an array from page to page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
crimius
Forum Commoner
Posts: 28
Joined: Sun Oct 13, 2002 6:02 pm
Location: Austin, Texas
Contact:

Problems passing an array from page to page

Post by crimius »

Page 1:

I create an array through a form's multiple SELECT list/menu like so:

Code: Select all

<select name="people[]" size="4" multiple>
<option value="14">name1</option>
<option value="4">name2</option>
<option value="8">name3</option>
<option value="3">name4</option>
</select>
and pass that array (using POST) to another page for display.


Page 2:

On page 2, I access that array using a FOR loop like so:

Code: Select all

$people[$i];

Page 3:

I can pass many variables from page 2 to page 3 using a form with the POST method, however, I can't seem to pass the array from page 2 to page 3.

I've passed the value of variables over multiple pages using a form's hidden input types, but I'm not sure how to do that with an array...or even if it's possible.

Basically, I'm not sure how to pass the array from page 1 to page 2 and then on to page 3. I feel like I'm missing something basic :?

Thanks in advance for any assistance,
-Craig
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

Post by Sevengraff »

You will probally want to use sessions. I don't use 'em much myself, but I think this will work:

page2.php

Code: Select all

<?
session_start();
$_SESSION['people'] = $_POST['people'];
// print your form stuff
?>
page3.php

Code: Select all

<?
session_start();
echo 'On page one you picked for people';
foreach($_SESSION['people'] as $key => $value) {
echo $key . '--' . $value . '<br>';
}
?>
or individually as $_SESSION['people'][n].
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

You can do it without session in html.

Code: Select all

<?
  for($t1=0;$t1<sizeof($people);$t1++){
    ?><INPUT TYPE="hidden" NAME="people[]" VALUE="<? echo $people[$t1] ?>"><?
  }
?>
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post by pootergeist »

the php functions for bringing an array to a string and back are serialize and unserialize - you can use them to fill a hidden field easily

<input type="hidden" name="str_array" value="' .serialize($array). '" />
...................
$array = unserialize($_POST['str_array']);
var_dump($array);

also useful for sessions and cookies.
crimius
Forum Commoner
Posts: 28
Joined: Sun Oct 13, 2002 6:02 pm
Location: Austin, Texas
Contact:

Post by crimius »

thanks for everyone's response. i'm going to try implementing the serialize / unserialize solution first. :)
crimius
Forum Commoner
Posts: 28
Joined: Sun Oct 13, 2002 6:02 pm
Location: Austin, Texas
Contact:

Post by crimius »

I'm having a bit of trouble getting unserialize to work.

On Page 2, I create the hidden form field like so:

Code: Select all

echo "<input type='hidden' name='serialized_people' value='".serialize($people)."'>";
Following is the HTML source (for Page 2) that is rendered:

Code: Select all

<input type='hidden' name='serialized_people' value='a:5:{i:0;s:1:"1";i:1;s:1:"2";i:2;s:1:"3";i:3;s:1:"4";i:4;s:1:"5";}'>
Here is the code on Page 3:

Code: Select all

$people = unserialize($_POST['serialized_people']); 
var_dump($people);	
	
$number_people = count($people);
echo $number_people;
Page 3 output:

bool(false) 1

-----------------------

So, var_dump($people); generates, "bool(false)"

and

echo $number_people; generates, "1"...there should be 5 integers contained in $people.

I'm not certain, but it seems like unserialize is not successfully converting the serialized string (from the Page 2 HTML source) back into a proper array.
crimius
Forum Commoner
Posts: 28
Joined: Sun Oct 13, 2002 6:02 pm
Location: Austin, Texas
Contact:

Post by crimius »

thanks []InTeR[], your solution is working well :)

-Craig
Post Reply