pass $_POST variable from one form to another

:-
Hello all. I’ve created a two-form page, and could use some advice on: either how to go about what I want, or else what’s a much easier way to do the following. I know that I could build a separate page for each function, but was hoping to keep it consolidated.

The first form on the page has a basic text input that creates an upload directory ($DirName) upon submitting (‘create’,$_POST).
The second form uploads photos to the newly named directory upon submitting (‘upload’,$_POST).
Both work individually, but I can’t seem to pass the new directory name to the upload form.

[ol][li]I’ve tried passing the variable through $_REQUEST. Works the same as $_POST, and disappears once 2nd $_POST script runs[/li]
[li]using $_GET in the first form successfully passes it along and even uploads photos. But, consequently it ALSO passes the mkdir() request to the first form again, generating a “File already Exists” error message.[/li][/ol]

Here’s what I’ve got:
[php]
define(‘UPLOAD_DIR’,’/beaten/path/’);

/---------------------BEGIN STEP 1. CREATE DESTINATION DIRECTORY -------------/
//mundane stuff has been left out
if (array_key_exists(‘create’,$_POST) && !empty ($POST[‘directoryName’])) {
$DirName = str_replace(’ ‘,’
’,$_POST[‘directoryName’]);
if(!is_dir(UPLOAD_DIR.$DirName)) {
$dir_success = mkdir(UPLOAD_DIR.$DirName);
}
else {
$create_warning = “$DirName already exists at this location. Either select it from the drop-down menu to add more photos, or give the new folder a unique name.”;
}
}//end if ‘creat’,$_POST

/----------------BEGIN STEP 2. UPLOAD FILES ----------------/

if (isset($DirName) && array_key_exists(‘upload’,$_POST)) {

//size & type check omitted for easy reading
if($sizeOK && $typeOK) {
switch($_FILES[‘image’][‘error’]) {
case 0:
//move the file to the upload folder and rename it
$success = move_uploaded_file($_FILES[‘image’][‘tmp_name’], UPLOAD_DIR.$DirName.’/’.time().$file);
if($success) {
$result = “”$file" has been uploaded successfully to $DirName.";
}// end if $success
else {
$result = “Error uploading “$file”. Please try again.”;
} //end else
break;
case 3:
$result = “Error uploading “$file”. Please try again.”;
default:
$result = “Sustem error uploading “$file”. Contact webmaster.”;
}//end switch
}//end if $sizeOK
elseif ($_FILES[‘image’][‘error’]==4) {
$result = “No file selected”;
}//end elseif
else {
$result = “”$file" cannot be uploaded.


  • Maximum size: 1MB (1024KB)

  • JPEG (jpeg, jpg, JPG) only

";
}//end else

}//end if ‘upload’,$_POST
if (array_key_exists(‘upload’,$_POST) && !isset($DirName)) {
$result= “There was a problem.”; //I KEEP GETTING THIS ERROR UPON (‘upload’,$_POST)
}

?>
[/php]

Image Folder Name:

  <?php
  // if uploaded, display $result
  if (isset($result)) {
	echo "<div id=\"success\"><p>$result</p></div>";  
  }
  ?>

Step 2: Upload Photos

Upload photo: *" />

Hi there,

Use the HTML’s HIDDEN form tag to forward the $_POST variable onto the next part.

<input type="hidden" name="DirName" value="<?php echo $_POST['DirName']; ?>">

This should allow you to send the DirName that you received from the first form and send it with the information you obtain from the second form so long as no other form fields in your second form doesn’t have the same name DirName.

Hope this helps.

OpzMaster,
First, thanks for the reply!! Of about 1.5 years of posts, this is the first time I’ve received a response before a couple of days past.

I just tried:

as well as:

in, both forms ( at different times to avoid misunderstanding, though it makes sense to be in the first form). And I tried it without “…&& isset($DirName)”. No luck so far. Been staring at this too long.

Hi again,

The HIDDEN tag should be on the page that the first form is pointing to as the information that you want to pass was from the first form. Are you adding the HIDDEN tag to the page that the first form is pointing to? Is the name in the HIDDEN tag the same name that you are referring to on the page that the second form points to? Are you using the correct $_POST information when passing the information to the HIDDEN field?

Let me know and I’ll be able to assist you further.

Got it!

Instead, I stored it in a $_SESSION variable like this:
[php]if ($POST1) {
$SESSION[‘DirName’] = str_replace(’ ‘,’’,$_POST[‘directoryName’]);
}
[/php]
and called it later
[php]
if ($POST2) {
//this now recalls it with the str_replace already set
$DirName = SESSION[‘DirName’];
}
[/php]
I’d tried something similar about an hour and a half ago. But, I was missing an ingredient somewhere. Don’t want to try to recreate. I already have a little bit of cleaning to do. THANKS!

Sponsor our Newsletter | Privacy Policy | Terms of Service