Passing Variables between Forms/Pages

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
corin
Forum Newbie
Posts: 8
Joined: Mon Nov 04, 2002 3:07 am

Passing Variables between Forms/Pages

Post by corin »

I've written a script so the user can upload an image ($filename) to the server. The image is input using a form, then checked for type (ie .jpg)/file size/and is displayed on the current page. In another form on the same page, the user can input their firstname and lastname. This is then posted to newpage.php and displayed. Basically I need to get the value of the first form into the second form (possibly as a hidden input) and then it will get POSTed with the info in form 2. Here is a sample of my code, with comments on what I am trying to achieve : Hope somebody can help me, thanks.

<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="50000">

<p>Upload Image: <input type="file" name="imgfile"><br>
<font size="1">Click browse to upload a local file</font><br>
<br>
<input type="submit" value="Upload Image">


/*== the script now checks the file type .jpg and uploads it and displays it on THIS page ==*/
<p>

</form>
<form action="newpage.php" method="post">

First Name :
<input type="text" name="firstname" />
<br />
Last Name :
<input type="text" name="lastname" />
<br />
<p>
<input name="submit" type="submit" value="Click to View Paper"/>
/*== the firstname and lastname are now posted to newpage.php and displayed ==*/
/*== the problem is, the image file ($filename) is not sent with it so I cannot display the image on newpage.php ==*/
</p>

</form>
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

you're not calling it by the right variable. the form uses name="imgfile" but you script uses $filename. those variables need to match. if you are using PHP version >4.1 then use this: $_FILES['imgfile']
corin
Forum Newbie
Posts: 8
Joined: Mon Nov 04, 2002 3:07 am

Post by corin »

You're right, however after the script has performed checks on the image file it is called $final_filename. I've sorted the problem now - it is a classic passing variables in forms case. So fixed it by POSTing the final_filename into the second form, then inputting it as "hidden" field. Not sure if it's the best way, though it now works. Superb.

<?php $_POST["final_filename"]; ?>

<input type="hidden" name="final_filename" value="<?php echo $final_filename?>"/>
Post Reply