【发布时间】:2021-08-19 01:39:45
【问题描述】:
提交文件后,现有文件出错后如何显示表单数据。我希望错误与之前输入的文档标题一起显示。现在,我可以在提交后显示错误,但标题输入为空。我希望保留键入的文档标题。怎么做?我在下面添加了 doc.php 和 code.php 的代码以供参考。希望这对我有所帮助。
<?php
//check error
if(isset($_SESSION['success']) && $_SESSION['success'] !='')
{
echo '<h2 class="alert alert-warning alert-dismissible fade show" role="alert"> '.$_SESSION['success'].'</h2> ';
unset($_SESSION['success']);
}
if(isset($_SESSION['status']) && $_SESSION['status'] !='')
{
echo '<h2 class="bg-danger text-white"> '.$_SESSION['status'].'</h2> ';
unset($_SESSION['status']);
}
?>
<form action="code.php" method="POST" enctype="multipart/form-data">
<hr style="border-top: 3px solid #ccc; background: transparent;">
<h5>Add Documents</h5>
<hr style="border-top: 1px solid #ccc; background: transparent;">
<div class="form-group" >
<label>Title</label>
<input type="text" name="doc_title" class="form-control" required>
</div>
<div class="form-group" >
<label>Documents </label>
<input type="file" name="doc" class="form-control" id="image" required>
</div>
<div class="float-right mb-2">
<button type="submit" name="adddoc" class="btn btn-primary">Save</button>
<a href="doc.php" class="btn btn-danger">Cancel</a>
</div>
</form>
code.php
if(isset($_POST['adddoc']))
{
$docTitle=$_POST['doc_title'];
$file=$_FILES['doc'];
$doc_name=$_FILES['doc']['name'];
$doc_tmpName=$_FILES['doc']['tmp_name'];
$doc_size=$_FILES['doc']['size'];
$doc_error=$_FILES['doc']['error'];
$doc_type=$_FILES['doc']['type'];
$docExt=explode('.', $doc_name);
$docActualExt= strtolower(end($docExt));
$doc_allow=array('pdf','xlsx');
if($doc_error === 0)
{
if($doc_size < 10000000)
{
$docDestination='upload/'.$doc_name;
move_uploaded_file($doc_tmpName, $docDestination);
$query="INSERT INTO documents (doc_title,doc) VALUES (?,?)";
$stmt=mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt,$query)){
mysqli_stmt_bind_param($stmt,"ss",$docTitle,$doc_name);
mysqli_stmt_execute($stmt);
$_SESSION['success']="Doc added";
header('Location: doc.php');
}
else{
$_SESSION['status']="Not added";
header('Location: adddoc.php');
}
}
else
{
$_SESSION['status']="The file is too big!";
header('Location: adddoc.php');
}
}
else
{
$_SESSION['status']="Error uploading file";
header('Location: adddoc.php');
}
}
【问题讨论】:
-
您期待什么类型的错误?您打算使用 AJAX 吗?你都尝试了些什么?什么有效,什么无效?请提供一个最小的、可重复的示例:stackoverflow.com/help/minimal-reproducible-example
-
你能显示code.php吗?
-
我添加了更多信息。希望这很清楚。谢谢。
标签: php html jquery mysql ajax