Page 1 of 1

Problems passing an array from page to page

Posted: Mon May 05, 2003 5:37 pm
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

Posted: Mon May 05, 2003 6:58 pm
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].

Posted: Tue May 06, 2003 1:59 am
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] ?>"><?
  }
?>

Posted: Tue May 06, 2003 3:41 am
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.

Posted: Tue May 06, 2003 11:49 am
by crimius
thanks for everyone's response. i'm going to try implementing the serialize / unserialize solution first. :)

Posted: Tue May 06, 2003 1:36 pm
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.

Posted: Wed May 07, 2003 11:53 am
by crimius
thanks []InTeR[], your solution is working well :)

-Craig