Page 1 of 1

Passing Variables between Forms/Pages

Posted: Mon Nov 04, 2002 3:07 am
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>

Posted: Mon Nov 04, 2002 10:37 am
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']

Posted: Mon Nov 04, 2002 8:43 pm
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?>"/>