gpt4 book ai didi

php - 如何在成功处理 PHP 脚本时打开模态框?

转载 作者:搜寻专家 更新时间:2023-10-31 21:47:12 26 4
gpt4 key购买 nike

我有 2 个文件,分别是 upload.phpprocess.php

在我的 upload.php 文件中,我有一个这样的表单:

<form action="upload.php" method="post" enctype="multipart/form-data">
<h5>Upload File</h5>
<p>.jpg and .png only.</p>
<input type="file" name="myfile" required> <br><br>

<h5>Choose action</h5>
<select class="form-control" name="action"required>
<option>No action (Finished)</option>
<option>Please Check</option>
</select><br>
<h5>Who are you</h5>
<select class="form-control" name="uploader" required>
<option>Person A</option>
<option>Person B</option>
<option>Person C</option>
</select><br>
<h5>Remark</h5>
<textarea name="remark" style="resize:none; width:100%; height:100px;">-</textarea><br><br>

<button type="submit" name="save" class="btn btn-dark btn-block">Upload</button><br>
</form>

在我的 process.php 中:

if (isset($_POST['save'])) { // if save button on the form is clicked
// name of the uploaded file
$filename = $_FILES['myfile']['name'];
$uploader = $_POST['uploader'];
$action = $_POST['action'];
$remark = $_POST['remark'];

// destination of the file on the server
$destination = 'uploads/' . $filename;

// get the file extension
$extension = pathinfo($filename, PATHINFO_EXTENSION);

// the physical file on a temporary uploads directory on the server
$file = $_FILES['myfile']['tmp_name'];
$size = $_FILES['myfile']['size'];

if (!in_array($extension, ['jpg','png'])) {
echo '<script language="javascript">';
echo 'alert("Upload failed! Your file extension must be .jpg or .png")';
echo '</script>';
} elseif ($_FILES['myfile']['size'] > 1000000) { // file shouldn't be larger than 1Megabyte
echo "File too large!";
} else {
// move the uploaded (temporary) file to the specified destination
if (move_uploaded_file($file, $destination)) {
$sql = "INSERT INTO files (name, size, uploader, action, remark, downloads) VALUES ('$filename', $size, '$uploader', '$action', '$remark', 0)";
if (mysqli_query($conn, $sql)) {
echo '<script language="javascript">';
echo 'alert("File uploaded successfully.")';
echo '</script>';
}
} else {
echo '<script language="javascript">';
echo 'alert("Failed to upload file.")';
echo '</script>';
}
}
}

如何实现:如果文件上传成功,一个模态框即looks like this会显示上传成功吗?现在,就像您在代码中看到的那样,我只让它显示一个警报。

最佳答案

我想我只是解决了我自己的问题。

我在 upload.php 中添加了模式,然后我添加了这个 javascript,这样我就可以通过直接 url 触发模式框。

<script type="text/javascript">

$(document).ready(function() {

if(window.location.href.indexOf('#success') != -1) {
$('#success').modal('show');
}

});
</script>

然后在process.php中,如果上传成功,重定向到mywebsite.com/upload.php#success

header(Location: 'upload.php#success')

如果谁有更好的解决方案,欢迎分享。 :)

关于php - 如何在成功处理 PHP 脚本时打开模态框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56875522/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com