Trying to make html page for uploading a file. I created a upload.cgi for the process. And added below code to my html page:
<form id="uploadForm" action="https://gpstest.surveybook.net/upload/upload.cgi" method="post" enctype="multipart/form-data">
<label for="file_upload">Select a file:</label>
<input type="file" name="file_upload" id="file_upload" onchange="uploadFile()"><br>
</form>
<script>
$(document).ready(function(){
function uploadFile() {
// Get the form element
var form = document.getElementById('uploadForm');
// Submit the form
form.submit();
}
})
</script>
It should upload when a file selected but i get error (in console) : "Uncaught ReferenceError: uploadFile is not defined"
Is it possible that i get this error because cant access to the upload.cgi, if not i am out of ideas and appreciate any help.
Thanks in advance.
3
Answers
The error "Uncaught ReferenceError: uploadFile is not defined" is occurring because the uploadFile function is defined inside the $(document).ready function, which makes it not accessible in the global scope. The onchange attribute in your HTML is trying to access uploadFile from the global scope, hence the error.
To fix this, you should move the uploadFile function outside of the $(document).ready function.
You’re encountering an issue because you’re defining a function after the DOM has loaded. Here, you have two options for updating your script:
or
Just remove $(document).ready , You don’t need that here