【问题标题】:File Upload and input text fields together文件上传和输入文本字段一起
【发布时间】:2012-12-17 07:15:35
【问题描述】:

我的表单中有文件上传和文本输入字段,我想要在同一页面上显示文件上传的错误消息,如果有错误不要转到下一页...如果有错误在文件上传时,还将文本字段的值返回到同一页面......我该怎么做?

<form action="send.php" method="post"  enctype="multipart/form-data">
<input type="hidden" name="submited" value="true" /><br>
<label for="file">Choose Photo:</label>
<input type="file" name="file" required><br>
First Name:<input type="text" name="fname" required><br>
Last Name:<input type="text" name="lname" required><br>
Choose Username:<input type="text" name="username" required><br>
Age:<input type="text" name="age" required><br>
<input type="submit" value="Submit" name="submit" >

</form>
</body>
</html>

这是文件上传的 php 代码....我将把数据从 &lt;input type="text"&gt; 插入到数据库中,我现在没有代码...

    <?php
ini_set( "display_errors", 0);
if(isset($_REQUEST['submited'])) {

// your save code goes here

$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2097152)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "";

if (file_exists("images/" . $_FILES["file"]["name"]))
{
echo "<font color='red'><b>We are sorry, the file you trying to upload already exists.</b></font>";
  }

else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"images/" . $_FILES["file"]["name"]);
echo "<font color='green'><b> Success! Your photo has been uploaded.</b></font>";
}
}
}
else
{
echo "<font color='red'><b>We are sorry, the file you trying to upload is not an image or it exceeds 2MB in size.</b></font><br><font color='blue'><i>Only images under size of 2MB are allowed</i></font>.";
}
}
?>

【问题讨论】:

  • 就个人而言,我会将 PHP 包装在一个函数中,并使用 AJAX 进行文件上传调用,这将允许 javascript 解析响应(返回语句)。然后,您可以使用 javascript 来显示该错误(如果发生)以及异步上传而不是原版文件上传!

标签: php html


【解决方案1】:

如果发生错误,只需让您的 PHP 重新生成填写了字段的表单(即 sst 值属性)。

【讨论】:

    【解决方案2】:

    嘿,

    要保持在同一页面上,您可以删除该操作

    发送.php

    然后将此处显示的 html 和 php 代码添加到同一个文件中。执行该文件时,它将停留在同一页面上。

    【讨论】:

    • 我可以这样做,但问题是无论图像是否成功上传,它都会将文本数据发送到数据库。我希望文本字段中的数据与图像一起发送。 .如果上传图片出错,文本数据将不会发送到数据库
    • 如果我明白你的意思,你可以试试这个: if($_FILES['logo']['name'] == "") { // 没有选择上传文件,你的 (重新)行动在这里}
    【解决方案3】:
    <?php
        if (!empty($_POST)){
            //send.php code
        }
    ?>
    
    <form action="send.php" method="post"  enctype="multipart/form-data">
        <input type="hidden" name="submited" value="true" /><br>
        <label for="file">Choose Photo:</label>
        <input type="file" name="file" required><br>
        First Name:<input type="text" name="fname" required><br>
        Last Name:<input type="text" name="lname" required><br>
        Choose Username:<input type="text" name="username" required><br>
        Age:<input type="text" name="age" required><br>
        <input type="submit" value="Submit" name="submit" >
    </form>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-11
      • 1970-01-01
      • 1970-01-01
      • 2020-03-14
      • 1970-01-01
      • 2015-07-21
      • 1970-01-01
      • 2014-01-22
      相关资源
      最近更新 更多