表单不会重定向到html中的另一个页面

时间:2014-01-30 23:20:32

标签: php html forms file-upload

我有这个表单用于在名为add.php的页面上传文件:

<!DOCTYPE html ISO-8859-1>
<?php
include("header.php");
include("verbinding.php");
if (!isset ($_SESSION["Logged_In"])) 
    {
        echo header('Location: http://ik42.webdb.fnwi.uva.nl/index.php'); ;
    } 

if (isset ($_SESSION["Admin"]))
    {
    if (!$_SESSION["Admin"] == 1)
        {
            echo        header('Location: http://ik42.webdb.fnwi.uva.nl/index.php'); ;
        }   
    }   
    // clear the value strings
    $Product = $Afbeelding = $ps4 = $xboxone = $WiiU = $pc = $genre = $prijs = $prijsID = $beschrijving = "";
?>

<h1> Welkom Admin</h1>

    <br /><br /><hr color="#000000" width="800" ><BR><center> Wil je een product toevoegen?</center><br /><br />
            <form name="input" action="add.php" method="POST">
                <table id="add">
                <tr>
                    <td><b>  Producten Toevoegen </b></td>
                </tr>
                <tr>
                    <td> <em>Product</em></td>
                        <td> <input type="text" name="Product" value="<?php echo $Product; ?>"> </td>
                </tr>
                <tr>
                    <td> <em>Ps4</em> </td>
                        <td> <input type="text" name="ps4" value="<?php echo $ps4; ?>"> </td>                   
                </tr>
                <tr>
                    <td> <em>Xbox One</em> </td>
                        <td> <input type="text" name="xboxone" value="<?php echo $xboxone; ?>"> </td>
                </tr>
                <tr>
                    <td> <em>Wii U</em> </td>
                        <td> <input type="text" name="WiiU" value="<?php echo $WiiU; ?>"> </td>         
                </tr>
                <tr>
                    <td> <em>PC</em> </td>
                        <td> <input type="text" name="pc" value="<?php echo $pc; ?>"> </td>         
                </tr>
                <tr>
                    <td> <em>Genre</em> </td>
                        <td> <input type="text" name="genre" value="<?php echo $genre; ?>"> </td>           
                </tr>
                <tr>
                    <td> <em>Prijs</em> </td>
                        <td> <input type="text" name="prijs" value="<?php echo $prijs; ?>"> </td>           
                </tr>
                <tr>
                    <td> <em>PrijsID</em> </td>
                        <td> <input type="text" name="prijsID" value="<?php echo $prijsID; ?>"> </td>           
                </tr>
                <tr>
                    <td> <em>Beschrijving</em> </td>
                        <td> <input type="text" name="beschrijving" value="<?php echo $beschrijving; ?>"> </td>         
                </tr>
                    <td> <input type="submit" value="Verzenden">
                </tr>
                <tr>
                    <td><form action="upload_file.php" 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></td>
                </tr>
                </table>
                <br></br><br></br>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if(!empty($_POST["Product"])){
    if( isset($_POST["Product"])) {

    $sql="INSERT INTO Producten (Product, PS4, XboxOne, WiiU, PC, Genre, Prijs, PrijsID, Beschrijving)
    VALUES
    ('$_POST[Product]','$_POST[ps4]','$_POST[xboxone]','$_POST[WiiU]','$_POST[pc]','$_POST[genre]','$_POST[prijs]','$_POST[prijsID]','$_POST[beschrijving]')";  
    $count = $db->exec($sql);
        if( $count == 1 ) {
            echo "Registratie voltooid.";
            echo "U kunt nu inloggen.";
        } else {
            echo "Registratie mislukt, neem contact op met de administrator als dit probleem blijft.";
        }

}}}
?>

        <?php
            include("footer.php");
            ?>      
        </div>

    </body>
</html>

但是当我点击提交按钮时,我甚至没有被重定向到upload_file.php,我只是回到同一页面add.php。我在这个页面上有另一个表单,它使用method =“POST”,并且会重定向到add.php页面。任何帮助将不胜感激。

upload_file.php:

<?php    
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_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 "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("/var/www/upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "/var/www/upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "/var/www/upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

1 个答案:

答案 0 :(得分:0)

您的表单嵌套在<form name="input" action="add.php" method="POST">内,并且该操作是执行的操作。

基本上它看起来像这样:

<form name="input" action="add.php" method="POST">
...
    <form action="upload_file.php" method="POST"
                        enctype="multipart/form-data">
             ...
        </form>
...
</form>

您需要删除<form name="input" action="add.php" method="POST">