|
|
|
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. |
|
| 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 | | | dynamic table columns Categories : PHP, HTML and PHP, Arrays, Databases, MySQL | | | 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 | | | Tag content retrieval from websites with preg_match Categories : PHP, Regexps, Arrays, HTML and PHP | | | Select with current month Categories : PHP, HTML and PHP, Date Time, Arrays | | | 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 | | | navbar.php3 - Dynamic hyperlinked navigation bars Categories : HTML and PHP, Arrays, PHP, Complete Programs | | | Parse string to find sub-string between two arbitrary strings Categories : PHP, Strings, HTML and PHP, Arrays | | | Parsing html tags with php. Get an array from this function Categories : PHP, HTML and PHP, Arrays, Tag Extractors | | | TreeView - Finally a working tree view function to be used as you want. Simple create the Table using the code provided and you will be able to have a tree view in your project. Download the zip to get the images. Categories : PHP, HTML and PHP, Navigation | | | Using PHP im HTML image tags Categories : PHP, HTML and PHP, Graphics, Beginner Guides | | | This script allows people to add their favorite quotes to your website. This
could easily be modified to be a guestbook script or comment page script. Categories : PHP, Complete Programs, HTML and PHP, Misc | | | How to preset a text string in a textarea input field Categories : HTML, HTML and PHP, PHP, Beginner Guides | |
| | | | 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
| |
|
|