Page 1 of 1
Passing an array in a hidden variable
Posted: Mon Jan 24, 2005 12:41 pm
by wadesmart
01242005 1242 GMT-6
I am passing an array onto another page but I have ran into some problems.
$array = $array();
$array[] = "one";
$array[] = "two";
$array[] = "three";
$var_serial = seralize($array);
$var_value = rawurlencode($var_serial);
input type='text" name="var_value" value=$var_value
On the receiving page:
$var_value = $_POST['var_value'];
$var_strip = stripslashes($var_value);
$var_serial = unserialize($var_strip);
My problem is this. Im echoing out everything before sending it and after receiving it so I can see what is up. When I try to unserialize I get
NOTICE: unserialize() Error at offset 0 of x bytes.
I have tried sending different data through but always get an error.
I did try not doing the rawurlencode() and that did nothing.
Wade
Posted: Mon Jan 24, 2005 12:49 pm
by feyd
might want to check the data you are getting to see if it is what was sent.. I'm assuming your example is mostly logic only, otherwise you have errors in that code.
Posted: Mon Jan 24, 2005 1:00 pm
by wadesmart
01242005 1258 GMT-6
I didnt make it clear what the problem was. My fault.
I am sending the data just find but when it unserialize() the data is incorrect.
Before:
a:4:{i:0;s:4:"Wade";i:1;s:5:"Kathy";i:2;s:8:"Chandler";i:3;s:5:"Smart";}
After:
a:2:{i:0;s:4:
I dont know why its being cut off after the colen.
Wade
Posted: Mon Jan 24, 2005 1:17 pm
by feyd
do you have an example of this online somewhere?
Posted: Mon Jan 24, 2005 1:21 pm
by wadesmart
No. Im working on a script on my computer.
Here is the actual code though and then a copy of the information from my screen.
page
Code: Select all
<?php
if(isset($_POSTї'Array'])) {
echo "<pre>";
print_r($_POST);
$var_value = $_POSTї'Smart_Family'];
echo "Unserializing.....<br />";
$temp = stripslashes($var_value);
$Smart_Family = unserialize($temp);
foreach($Smart_Family as $member) {
echo "<br />$member";
}
}
?>
<html>
<head>
</head>
<body>
<form method = "post" action = "<?php echo $_SERVERї'PHP_SELF']; ?>" >
<?php
echo "Creating array <br />";
$Smart_Family = array('Wade', 'Kathy', 'Chandler', 'Smart');
echo "Populating array = Smart <br />";
//$Smart_Familyї] = 'Wade';
//$Smart_Familyї] = 'Kathy';
//$Smart_Familyї] = 'Chandler';
echo "Serializing array...... <br />";
$Smart_Family = serialize($Smart_Family);
echo "The contents of the serialized array...<br />";
echo "<br />$Smart_Family <br />";
//echo "<br />Removing any non-required characters....<br />";
//echo "$Smart_Family <br />";
echo "Putting data in hidden input. <br />";
echo "<input type='hidden' name='Smart_Family' value=$Smart_Family>";
?>
Creating second array: Enter three names.
<input type="text" name="Nameї]"><input type="text" name="Nameї]"><input type="text" name="Nameї]">
<input type="submit" name="Array" value="two arrays sent">
</form>
</body>
</html>
And the screen contents:
Array
(
[Smart_Family] => a:3:{i:0;s:4:
[Name] => Array
(
[0] => one
[1] => two
[2] => one
)
[Array] => two arrays sent
)
Unserializing.....
Creating array
Populating array = Smart
Serializing array......
The contents of the serialized array...
a:4:{i:0;s:4:"Wade";i:1;s:5:"Kathy";i:2;s:8:"Chandler";i:3;s:5:"Smart";}
Putting data in hidden input.
Posted: Mon Jan 24, 2005 1:32 pm
by wadesmart
Ok. I got something.
I have been using echo to output the html but that isnt how I usually do it.
I normally do a $form_block and add things in as i need them. That was the problem.
I think I got it now.
Wade
Posted: Mon Jan 24, 2005 1:38 pm
by feyd
the problem is the quote marks that appear in the serialization. You'll need to scrub them out. Then add them back in once you get to the other side.
It may be better to pass this information through sessions..
you may want to try using single quotes around the value you are writing into the html... that might fix it easily
Posted: Mon Jan 24, 2005 1:39 pm
by JAM
I don't like posts about (un)serialize() as the function has been broken from time to time under certain versions, with numerous of fixes in the cvs's.
Actually, I'm not surprised if it'd break down again...
