I know that this question has been asked a lot of times but everything I tried does not work.
HERE HERE HERE HERE I've read many other posts and articles, nothing works.
So, what I'm trying to do is passing a variable to either the JS file or the HTML file.
This variable is "fileNewName", basically when an image is uploaded, it's name is replaced by a uniqid so that if two images have the same name one of them won't be overwritten.
I then need to get this "fileNewName" back to the HTML in order to display it with the img object. I want to avoid changing the index.HTML file to index.PHP, but I understand that from what I've read, I will have to do that at some point and probably use Ajax.
What I wanted to do originally is : pass the variable from PHP to set a var in JS with it's value
(like that : var name = "somehow get the the variable from the PHP file";
) and then use img.src = "uploads/" + name;
.
I hope that I've explained my problem clearly. Any help or hints will be much appreciated, thanks !
Here are the different codes :
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/index.css" >
<link rel = "icon" href="logo.png" type="image/x-icon">
</head>
<body>
<div class="topnav">
<img src="logo.png" style="width:40px;height:40px;"></img>
<a class="active" href="index.html">HOME</a>
<a href="help.html">HELP</a>
<a href="about.html">ABOUT</a>
<a href="examples.html">EXAMPLES</a>
</div>
<div class="dropzone" id="dropzone">
<img src="" id="img" class="img">
<form class="form" id="myForm">
<input type="file" id="inpFile" accept="image/png, image/jpg, image/jpeg" hidden="hidden">
<button type="button" id="custom">CHOOSE AN IMAGE</button>
<button type="submit" class="btn" id="btn">START SORTING</button>
</div>
<script src="drop.js" type="text/javascript"></script>
</body>
</html>
drop.js :
const realBtn = document.getElementById("inpFile");
const customBtn = document.getElementById("custom");
const inpFile = document.getElementById("inpFile");
const myForm = document.getElementById("myForm");
var btn = document.getElementById("btn");
btn.disabled = true;
realBtn.onchange = function(e) {
btn.disabled = false;
}
customBtn.addEventListener("click", function(click) {
inpFile.click();
});
myForm.addEventListener("submit", e => {
e.preventDefault();
const endpoint = "upload.php";
const formData = new FormData();
formData.append("inpFile", inpFile.files[0]);
console.log();
fetch(endpoint, {
method: "post",
body: formData
}).catch(console.error);
document.getElementById("btn").style.display = "none";
document.getElementById("custom").style.display = "none";
document.getElementById("img").style.display = "block";
var img = document.getElementById("img");
var fileName = ???;
img.src = "uploads/";
});
upload.php :
<?php
$uploadOk = 1;
$fileName = $_FILES["inpFile"]["name"];
$fileTmpName = $_FILES["inpFile"]["tmp_name"];
$fileExtsion = explode('.', $fileName);
$fileActualExtension = strtolower(end($fileExtsion));
$fileNewName = uniqid('').'.'.$fileActualExtsion;
$targetPath = "uploads/" . basename($fileNewName);
if ($_FILES["inpFile"]["size"] > 500000000) {
$uploadOk = 0;
}
if ($uploadOk == 1) {
move_uploaded_file($fileTmpName, $targetPath);
}
?>