|
|
|
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 :
| <FORM ACTION="MyScript.php" NAME="MyForm">
<SELECT NAME="MySelect[]">
<OPTION VALUE="1">Option 1</OPTION>
<OPTION VALUE="2">Option 2</OPTION>
<OPTION VALUE="3">Option 3</OPTION>
</SELECT>
</FORM> | |
This will create an Array called $MySelect in MyScript.php which will have the
following values :
| <?php
$MySelect[0] = "Option 1";
$MySelect[1] = "Option 2";
$MySelect[2] = "Option 3";
?> | |
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 :
| <FORM ACTION="MyScript.php" NAME="MyForm">
<INPUT TYPE="HIDDEN" NAME="MySelect[]" VALUE="<?=$MySelect?>">
</FORM> | |
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
unserialize() : This function takes a serialized value and returns back the
appropriate php value.
http://www.weberdev.com/unserialize
urlencode() : This function encodes any string to a valid URL format
http://www.weberdev.com/urlencode
urldecode() : This function decodes any URL encoded string.
http://www.weberdev.com/urldecode
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 :
| <?php
$MySelect = unserialize(urldecode($_POST["MySelect"]));
?> | |
Now we can use $MySelect as an Array in the new script. It will contain the values
that were in the array in the first script. |
|
| Select with current month Categories : PHP, HTML and PHP, Date Time, Arrays | | | Tag content retrieval from websites with preg_match Categories : PHP, Regexps, Arrays, HTML and PHP | | | dynamic table columns Categories : PHP, HTML and PHP, Arrays, Databases, MySQL | | | Print out array key => value in colored HTML Categories : PHP, Arrays, HTML and PHP | | | Form Submission Using Array's Categories : PHP, HTML and PHP, Beginner Guides, Arrays | | | Parsing html tags with php. Get an array from this function Categories : PHP, HTML and PHP, Arrays, Tag Extractors | | | navbar.php3 - Dynamic hyperlinked navigation bars Categories : HTML and PHP, Arrays, PHP, Complete Programs | | | PHP3: Formmail. Just a cgi formmail, but than in PHP. It is easy to use! Categories : HTML and PHP, Email, PHP, Perl, HTML and PHP | | | CSS style switcher Categories : PHP, CSS, HTML and PHP, Arrays, Sessions | | | Simple script to passing persistent and growing array between recalls of one page (manipulate little stack). Categories : Arrays, Global Variables, PHP, HTML and PHP, Variables | | | A simple class with some HTML output functions that would come in handy for consistent page layout etc. Categories : PHP, PHP Classes, HTML and PHP, HTML, Navigation | | | a function that builds an HTML select list from any mysql table. Categories : PHP, MySQL, HTML and PHP | | | Message of the Day - Random Message (Needs MySQL!) Categories : Databases, HTML and PHP, PHP, MySQL | | | Constantly refresh your PHP/HTML page data. Categories : PHP, HTML and PHP, Sybase | | | clearing variables in php3 Categories : Variables, Arrays, PHP | |
| | | | 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?
smita
| |
|
|
|