我的表单中有文件上传和文本输入字段,我想要的是在同一页面显示文件上传的错误信息,如果有错误则不要转到下一页...如果有错误在文件上传时还将文本字段的值返回到同一页面......我是怎么做到的?
<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代码....我将从<input type="text">
插入数据到数据库,我现在没有代码......
<?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>.";
}
}
?>
答案 0 :(得分:1)
如果发生错误,只需让你的PHP重新生成带有填写字段的表单(即sst值属性)。
答案 1 :(得分:0)
Heey,
要将其保留在同一页面上,您可以删除操作
send.php
然后在同一个文件中添加html和php代码。当该文件被执行时,它将保持在同一页面上。
答案 2 :(得分:0)
<?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>