【问题标题】:form doesn't redirect to another page in html表单不会重定向到 html 中的另一个页面
【发布时间】:2014-01-30 23:20:31
【问题描述】:

我有这个表格用于在名为 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 页面。任何帮助将不胜感激。

上传文件.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";
  }
?>

【问题讨论】:

  • 也许你的upload_file.php中有header("Location: add.php")。
  • 不,我没有。此页面上的其他表单不会受到伤害,对吧?
  • 我们能看到这个upload_file.php吗?
  • 好了,它就在上面。
  • 您的表单已损坏。在伪代码中它看起来像这样:
    content
    content

标签: php html forms file-upload


【解决方案1】:

您的表单嵌套在:&lt;form name="input" action="add.php" method="POST"&gt; 中,并且执行该操作。

基本上是这样的:

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

你需要删除&lt;form name="input" action="add.php" method="POST"&gt;

【讨论】:

  • 是的,但是 OP 的代码还有其他问题。这是你的全部;-)
  • 现在我确实被重定向了,但我得到:警告:move_uploaded_file(gameimg/Roland.jpg):打开流失败:权限被拒绝。这是否意味着我根本不允许上传任何内容?
  • 明天再试一次。现在得睡觉了。谢谢大家!
猜你喜欢
  • 1970-01-01
  • 2016-02-28
  • 2017-08-03
  • 1970-01-01
  • 1970-01-01
  • 2015-09-29
  • 2015-03-09
  • 2015-01-17
  • 2021-01-08
相关资源
最近更新 更多