Ok So i just wanted to remove the event listener from my uploadFiles() function in my js. Because I want the function to kick off only when I click that button and not any other button in the form as well ass preventing the listener from running twice if somebody closes the overlay and goes back into it without refreshing. Here is my code. Why do I keep receiving a promise error.
It is pretty vanilla stuff. Something I am just not seeing.The code that worls with the eventListener.
`
/* ----- Upload File / Image -----*/
function uploadFile(uid) {
const url = 'process.php';
const form = document.querySelector('form');
//let uid = document.getElementById('upid').value;
var data = {
auploadid: "1",
auserid: "1",
};
const btn = document.getElementById('newFiles');
//console.log("Run Onlick");
form.addEventListener('submit', e => {
//console.log("Run Heard The Click");
e.preventDefault();
const files = document.querySelector('[type=file]').files;
const formData = new FormData();
for (let i = 0; i < files.length; i++) {
let file = files[i];
//console.log(uid);
formData.append('files[]', file);
formData.append('uploadid', uid);
}
fetch(url, {
method: 'POST',
body: formData
}).then(response => {
return response.text();
}).then(data => {
console.log(data);
});
});
return;
}
And this is the code that throws the error. Again, all I am trying to do is remove the event Listener.
/* ----- Upload File / Image Without Listener-----*/
function uploadFiles(uid) {
const url = 'process.php';
//const form = document.querySelector('form');
const form = document.getElementById("uploadForm");
//e.preventDefault();
const files = document.querySelector('[type=file]').files;
const formData = new FormData();
for (let i = 0; i < files.length; i++) {
let file = files[i];
//console.log(uid);
formData.append('files[]', file);
}
formData.append('uploadid', uid);
fetch(url, {
method: 'POST',
body: formData
}).then(response => {
return response.text();
}).then(data => {
console.log(data);
});
return;
}