teslasimus
teslasimus

Reputation: 1298

multiple forms in html with enctype="multipart/form-data"

I'm trying to send two forms, one with enctype="multipart/form-data" for file upload, the other without. my problem is only one of them is working. the submit button wont send two form.

What I've tried:

<?php
echo '<form method="post" action="' . $PHP_SELF . '">';

....

echo '<input type="submit" name="submit" value="Submit"><br>';
echo "</form>";

echo '<form action="' . $PHP_SELF . '" method="post" enctype="multipart/form-data"> 
      <label for="file">Filename:</label>
      <input type="file" name="file" id="file"><br>
      <input type="submit" name="submit" value="Submit"></form>';

if (isset($_FILES["file"]["name"])){
    $file_name = $_FILES['file']['name'];

    if ($_FILES["file"]["error"] > 0) {
        echo "Error: " . $_FILES["file"]["error"] . "<br>";
    } else {
        echo "Upload: " . $_FILES["file"]["name"] . "<br>";
        echo "Type: " . $_FILES["file"]["type"] . "<br>";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
        echo "Stored in: " . $_FILES["file"]["tmp_name"];
        move_uploaded_file($_FILES['file']['tmp_name'], '../images/' . $file_name);
    }
}
?>

Upvotes: 1

Views: 4607

Answers (2)

John Peter
John Peter

Reputation: 2928

Just Try With The Following :

<?php
echo '<form method="post" action="' . $PHP_SELF . '">';

....

echo '<input type="submit" name="Submit_Button_One" value="Submit"><br>';
echo '</form>';

echo '<form action="' . $PHP_SELF . '" method="post" enctype="multipart/form-data"> 
      <label for="file">Filename:</label>
      <input type="file" name="file" id="file"><br>
      <input type="submit" name="Submit_Button_Two" value="Submit"></form>';

/*First Form Submit*/

if(isset($_POST['Submit_Button_One']))
{
$dataValue = $_POST['data'];    
}

/*Second Form Submit*/

if(isset($_POST['Submit_Button_Two']))
{
  if(isset($_FILES["file"]["name"])){

     $file_name = $_FILES['file']['name'];

     if($_FILES["file"]["error"] > 0) {
        echo "Error: " . $_FILES["file"]["error"] . "<br>";
     }  else {
        echo "Upload: " . $_FILES["file"]["name"] . "<br>";
        echo "Type: " . $_FILES["file"]["type"] . "<br>";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
        echo "Stored in: " . $_FILES["file"]["tmp_name"];
        move_uploaded_file($_FILES['file']['tmp_name'], '../images/' . $file_name);
     }
   }
}

?>

I think this may help you to resolve your problem.

Note : Give Different name for submit buttons. Then it's possible to get the datas as you wish from both the forms.

Upvotes: 1

fdreger
fdreger

Reputation: 12505

This is how it works in HTML. You can only send one form at a time. The only workaround is to send all your data with just one form.

Upvotes: 4

Related Questions