Process multiple forms in one PHP file

Hello. I'm trying to make a "image resizer" which first uploads a file to my server, then resizes it.

It works fine if I remove the second form and add the the inputs to the the first form, but then I can't show the original image's size dimensions, since it needs to be uploaded first as far as I know...
What should I do to make the second form work?
<html>
<head>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file"><br />
<input type="submit" name="submit" value="Continue"><br />
</form>
</body>
</html>

<?php
if(isset($_POST['submit'])){
if ($_FILES["imgsource"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["imgsource"]["error"] . "<br />";
    }
  else
    {
      $tmpdir = 'temp';
      $gendir = 'generated';
      $fname = $_FILES["file"]["name"];
      $tmpname = $_FILES["file"]["tmp_name"];
      $file_type = $_FILES["file"]["type"];
      move_uploaded_file($tmpname,$tmpdir."/".$fname);
      list($width, $height) = getimagesize($tmpdir."/".$fname);
      $newfname = md5(time());
      echo "<br />Original Image: <br /><img src='".$tmpdir."/".$fname."'> <br /> Image size: ".$width." x ".$height."<br /><br />"; 
      echo '<form action="" method="post">
            New Height: <br />
            <input type="text" name="imgh"><br /><br />
            New Width: <br />
            <input type="text" name="imgw"><br /><br />
            <input type="submit" name="submit2" value="Confirm resize">
             </form>';
if(isset($_POST['submit 2'])){ 
         $imgh = $_POST['imgh'];
         $imgw = $_POST['imgw']; 
         if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
         $new_img = imagecreatefromjpeg($tmpdir."/".$fname);
         }elseif($file_type == "image/x-png" || $file_type == "image/png"){
         $new_img = imagecreatefrompng($tmpdir."/".$fname);
         }elseif($file_type == "image/gif"){
         $new_img = imagecreatefromgif($tmpdir."/".$fname);
         }

      $resized_img = imagecreatetruecolor($imgw,$imgh);
      imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $imgw, $imgh, $width, $height);
      ImageJpeg($resized_img,$gendir."/".$newfname);
      ImageDestroy ($resized_img);
      ImageDestroy ($new_img);
      echo "<img src='".$gendir."/".$newfname."'> <br /> Should work!";
}
}
}
?>

Open in new window

LVL 2
Ante0Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
You can't process two forms from a page - only one gets submitted

Either

1. Combine them
2. Put hidden variables in the second one with width and height values stored there using same field names as for the first form
3. Look at an AJAX upload tool that uploads the file and returns the metrics to the calling script which then updates the relevant fields in the page.

I would recommend 2 in this case
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ante0Author Commented:
Yes, I will have to go with 2 or 3... as I want to show the original image and it's size before confirming and resizing it.
Thanks :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.