Page 1 of 1

Tough problem: Serializing a multiple select array

Posted: Thu Apr 22, 2004 1:22 am
by Lord Sauron
Hello array freaks,

Last Tuesday I had a discussion with Mark about passing a multiple select array through several forms (viewtopic.php?t=21223). Marks help was very welcome, but the problem is unfortunately still not completely solved. I was hoping one of you could help me with it.

Summarizing the discussion, Mark told me, I had to serialize my title-array (one person can have several titles and therefore this array comes from a multiple select list), before posting my second form. I tried to do so, but when I am printing my arrays, it looks like this:

// After posting it from the first form -> everything OK
Array ( [0] => Prof [1] => Mr )

// The data arrives as it should at the second form, but after posting it
// from the second form, it isn't OK anymore. I tried two options:

OPTION 1: Adding the title-array as a hidden value to the second form
// After posting and before unserializing
Array ( [0] => a:3:{i:0;s:4: )
// After unserializing
Array ( [0] => a:3:{i:0;s:4: [1] => )

// By the way, the code of the hidden field is as follows:
ECHO "<INPUT TYPE=\"hidden\" NAME=\"title[]\" VALUE=\"".serialize($titel)."\">";


OPTION 2: Using serialize($title) directly in the action-command
// After posting and before unserializing
Array ( [0] => a:3:{i:0;s:4: )
// After unserializing
Array ( [0] => serialize(Array) [1] => )


Any idea what could be wrong this time? How can I get the original array-data back?

Thanks already for the answer,

Antonie

Re: Tough problem: Serializing a multiple select array

Posted: Thu Apr 22, 2004 3:20 am
by amnuts
Hi,

Once you have serialized your array, you don't actually need to pass that value as an array anymore. So in your hidden field, drop the '[]', and make it look like this:

echo '<input type="hidden" name="title" value="', serialize($title), '">';

Then in your code, when you unserialize, you should have the expected array.

When I do this, I also base64_encode the string so that I know it's going to have safe values in the HTML. Such as:

echo '<input type="hidden" name="title" value="', base64_encode(serialize($title)), '">';

and then when I come to extract the data I do:

$title = unserialize(base64_decode($_POST['title']));

Give it a whirl and see if it works.

Andy

Thanks!!!

Posted: Thu Apr 22, 2004 8:12 am
by Lord Sauron
Hi Andy,

Thanks a lot. The base64_encode did the trick. Although I'm not really understanding what it exactly does.
This really helped a lot. I had this problem for about 2 months now. After 3 or 4 days of irritation, I did leave it. But 3 days ago, it came up again.
You saved me a lot of trouble, so hopefully I will ever be able to pay you back, by helping you with a problem.

Kind regards.

By the way...

Posted: Thu Apr 22, 2004 8:45 am
by Lord Sauron
By the way,

I'm lucky to use action instead of onClick, for posting my form.

I really wouldn't know how to do it with onClick. Maybe something like:

onClick=\"window.location.href='chooseForm.php?titel=".base64_decode(serialize($titel))."';\"

No idea if this syntax could work. Just curious

Re: By the way...

Posted: Thu Apr 22, 2004 8:50 am
by twigletmac
Lord Sauron wrote:I really wouldn't know how to do it with onClick. Maybe something like:

onClick="window.location.href='chooseForm.php?titel=".base64_decode(serialize($titel))."';"

No idea if this syntax could work. Just curious
Na, PHP is server-side and an onClick event is client-side so you wouldn't be able to run a PHP function via an onClick event.

Mac

It IS possible

Posted: Thu Apr 22, 2004 9:46 am
by Lord Sauron
I think it is possible. I'm doing this at the moment already, and it works:

ECHO "<TD><INPUT TYPE=\"submit\" VALUE=\"change Data\" NAME=\"changeDataButton\" TABINDEX=\"3\" onClick=\"window.location.href='chooseForm.php?phone=".rawurlencode($phone)."&mobile=".rawurlencode($mobile)."&fax=".rawurlencode($fax)."';\"></TD>\n";