Passing a multiple select array through several forms

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Lord Sauron
Forum Commoner
Posts: 85
Joined: Tue Apr 20, 2004 5:53 am
Location: Tilburg, NL

Passing a multiple select array through several forms

Post by Lord Sauron »

Hello array experts,

Tuff question I guess. At the moment I'm experiencing problems with passing a multiple select single dimensional array through several forms.
This means, I do have a form in which I create the array. As follows (of course simplified):

// Script1----------------------------------------------------------


ECHO "<TABLE>";
ECHO "<FORM NAME=\"customerForm1\" METHOD='POST' ACTION=\"chooseForm.php?title=$title\">\n";

// ....
ECHO "<TR>";
ECHO "<TD><SELECT NAME='title[]' MULTIPLE>\n";
fillArray("Prof",$title);
fillArray("Master",$title);
fillArray("Engineer",$title);
// etc.
ECHO "</TD>";
ECHO "</TR>";
// ....

ECHO "</FORM>";
ECHO "</TABLE>";

// ---------------------------------------------------------------

So far no problem. The array gets filled as it should be done, and arrives perfectly at chooseForm.php. In chooseForm I do call another function in another php script, which contains another form. This looks like this:

// Script2--------------------------------------------------------

function createSecondForm($title) {

ECHO "<TABLE>";
ECHO "<FORM NAME=\"customerForm2\" METHOD='POST' ACTION=\"postForm.php?title=$title\">\n";

// .....

ECHO "</FORM>";
ECHO "</TABLE>";

}

// ---------------------------------------------------------------

The title-array even arrives perfectly at script2. But, the problems are there as soon as I'm trying to post customerForm2. The contents of the array are gone, and instead becomes $title an empty array except for the text "array" at position 0 of this array.

I really don't understand, how it is possible that posting the array from customerForm1 is no problem, but is not working when posting from customerForm2. I even tried to use <INPUT TYPE=\"hidden\" NAME='title[]' VALUE='$title'> in customerForm2, but that didn't have any effect.

I would appreciate any help.

Kind regards,

Antonie
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

First of all, please read this - viewtopic.php?t=21171

As for your problem.

When you submit the first form, obviously the data gets passed as an array to the next page to you can do with it whatever you want, when you submit the next form, the only data you submit, it the data from the second form. You haven't told it to pass any other information.

I suggest if you want to pass variable across a number of page, you should look into the use of sessions.

Mark
User avatar
Lord Sauron
Forum Commoner
Posts: 85
Joined: Tue Apr 20, 2004 5:53 am
Location: Tilburg, NL

That's an idea

Post by Lord Sauron »

Hi Mark,

Mmm, although I don't like to 'abuse' my session array, your answer could be a solution. Thanx. Don't understand why I haven't thought about it myself.

However, I tried to post the title array through the second form by means of a hidden value. As far as I'm understanding forms, this should work, but unfortunately it doesn't. I will try your solution.

Kind regards,

Antonie
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Yes, you can pass through a hidden filed but you will need to use the serialize function to create a storable value.

Then, you need to unserialize on the next page.

http://se.php.net/manual/en/function.serialize.php

Mark
User avatar
Lord Sauron
Forum Commoner
Posts: 85
Joined: Tue Apr 20, 2004 5:53 am
Location: Tilburg, NL

Almost working

Post by Lord Sauron »

It seems, that this is the right way, although I'm still doing something wrong.

When I am printing my arrays, it looks like this:
// After posting it from the first form.
Array ( [0] => Prof [1] => Mr )
// After posting it from the second form, but before unserializing
Array ( [0] => a:3:{i:0;s:4: )
// After unserializing, using a hidden value in the second form
Array ( [0] => a:3:{i:0;s:4: [1] => )
// After unserializing, using serialize($title) directly in the action-command
Array ( [0] => serialize(Array) [1] => )

I guess, the solution of using a hidden value is the best solution. It looks like this:
ECHO "<INPUT TYPE=\"hidden\" NAME=\"title[]\" VALUE=\"".serialize($titel)."\">";

Any idea what could be wrong this time?

Antonie
User avatar
Lord Sauron
Forum Commoner
Posts: 85
Joined: Tue Apr 20, 2004 5:53 am
Location: Tilburg, NL

Passing arrays with serialize function

Post by Lord Sauron »

Off course I could try to use one of the solutions mentioned in this forum, but they don't seem very nice solutions to me.

http://forums.devarticles.com/showthread.php?p=8310
Post Reply