working with multible 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
terji
Forum Commoner
Posts: 37
Joined: Tue May 14, 2002 5:27 pm
Location: Denmark

working with multible forms ?

Post by terji »

I need some help working with a form containing a form :?
I'm coding the typical compose email gui. After entering e-mail, title and the message itself I want to give the user the option to attach up to several files. When pressing [attach file] the user is directed to an "upload/attach file" screen. When done, the user returns to the [compose email] screen and can either attach another file or submit the e-mail.
How can I solve this problem? Should I use a form within a form?

How do I keep track of the "e-mail" data, when going of to "attach file" and back?
User avatar
amnuts
Forum Newbie
Posts: 16
Joined: Fri Jun 14, 2002 1:48 pm
Contact:

Post by amnuts »

You could serialize the $_POST information into a hidden form field. Then when you need it again just unserialize it into another array. You'll probably need to htmlentities it as well, such as:

<input type="hidden" name="data" value="<?php echo htmlentities(serialize($_POST)); ?>">

You could go one more step than that to make it a little harder to read in the view source, such as:

<input type="hidden" name="data" value="<?php echo htmlentities(base64_encode(gzcompress(serialize(($_POST))))); ?>">

Andy
terji
Forum Commoner
Posts: 37
Joined: Tue May 14, 2002 5:27 pm
Location: Denmark

hmmmm

Post by terji »

I'm a newbie

I do however see what you mean, but I would be greatful if you could take it down to "step by step" :oops:
User avatar
amnuts
Forum Newbie
Posts: 16
Joined: Fri Jun 14, 2002 1:48 pm
Contact:

Post by amnuts »

Okay, let's try to break this down.

Assuming you have 3 pages. Page 1 sends the initial form data to page 2. Page 2 sends the attachement data to page 3. Page 3 processes and send the email. What you want to do is retain the information from page 1 in page 2 and then use all of the data in page 3.

This is a VERY simplified example, but will give you something to go off:

page1.php

Code: Select all

<form action="page2.php" method="post">
<input type="text" name="user_name"><br>
<input type="text" name="send_to"><br>
<textarea name="message" rows="5" cols="40"><textarea><br>
<input type="submit" value="go to next stage">
</form>
page2.php

Code: Select all

<form action="page3.php" method="post" enctype="multipart/form">
<input type="hidden" name="previous_data" value="<?php echo base64_encode(gzcompress(serialize(($_POST)))); ?>"> 
<input type="file" name="attachement"><br>
<input type="submit" value="go to next stage">
</form>
page3.php

Code: Select all

<?php

$initial = unserialize(gzuncompress(base64_decode($_POST&#1111;'previous_data'])));
// do any and all processing and mail sending here

?>
So now on page3.php you have the variable '$initial' which is an array with anything that was in the original '$_POST' variable. So you can access the data such as:

Code: Select all

<?php

echo $initial&#1111;'user_name'], "<br>\n";
echo $initial&#1111;'send_to'], "<br>\n";
echo $initial&#1111;'message'], "<br>\n";

?>
Hope that helps.

Andy
Post Reply