When you want to pass the contents of a multiple select box for example from an HTML
Form to a PHP Script you usually simply add the [] to the variable name like this :
Now, suppose you want to send this array to another PHP Script via an HTML form, how
would you do it?
Doing the same thing as in the 1st form will not work since if you put $MySelect in
the VALUE of the input field that should pass the value on to the script, all you
will get is the String "Array" and not the actual contents of the Array. This is
cause the submission is done from the HTML and not in PHP and the actual value that
the HTML sees is the actual value that PHP printed into the VALUE="" which would be
the String "Array" if we do something like :
There is a pretty simple solution for this. We need to convert the Array into a
string, submit it as a string and bring it back from a string to an Array in the new
PHP Script. Think this is complicated? Think again. There are 4 functions in PHP
that do this work for you :
serialize() : This function takes a value and returns a string that represents that
value and can be stored in this string formation. http://www.weberdev.com/serialize
What we can do now are two very simple steps. In the first script, we would do :
<?php
//Serialize and encode the Array to make it a simple string.
$MySelect = urlencode(serialize($MySelect));
?>
<FORM ACTION="MyScript1.php" NAME="MyForm1" METHOD="POST">
<INPUT TYPE="HIDDEN" NAME="MySelect" VALUE="<?=$MySelect?>">
</FORM>
The MyScript1.php script will get the $MySelect variable as a string. To convert it
back to an Array we need to the opposite of what we did before :
smita manohar wrote :868
im not getting the desire result using above functions. in the 2nd page where you are using unserialize function, to get back the array, i used count($arrayname) it showed 0 also when i used
while (list($key, $value) = each ($arrayname) )
{ echo $value; }
i got error as it is not valid array.
for the statement which u have shown,
$MySelect = unserialize(urldecode($MySelect));
when i tried
$MySelect[] = unserialize(urldecode($MySelect));
it showed count($MySelect) = 1
but when i tried to print the array $MySelect it was showing result as `Array`
can u show the another example using these functions?