如何在php中输入按钮文件并同时发送到服务器



在我的代码中,我有两个按钮。第一个是文件输入,另一个是将该文件发送到服务器。我的网站上只有一个按钮怎么能做到这一点?她是我的php代码:

const realFileBtn = document.getElementById("real-file");
const customBtn = document.getElementById("custom-button");
customBtn.addEventListener("click", function() {
realFileBtn.click();
});
<form method="POST" action="upload.php" enctype="multipart/form-data">
<input type="file" name="file" id="real-file" hidden="hidden">
<input type="submit" value="Göndər" id="custom-button">
</form>
<script src="./script.js"></script>
<link rel="stylesheet" href="./style.css">
<?php
$files = scandir("uploader");    
?>

到此为止。

const fileInput = document.getElementById('file-input')
const button = document.getElementById('custom-button')
fileInput.onchange = (e) => {
console.log(e)
document.getElementById('my-form').submit()
};
button.addEventListener('click', (e) => {
e.preventDefault();
fileInput.click()
});
<form method="POST" action="upload.php" id="my-form" enctype="multipart/form-data">
<input type="file" name="file" id="file-input" hidden="hidden">
<button value="Göndər" id="custom-button">Gönder</button>
</form>

当更改了文件时,您可以调用表单提交

document.getElementById("real-file").onchange = function() {
document.getElementById("form").submit();
};

最新更新