php - Trying to send post data via JavaScript in conjunction with my upload
Get the solution ↓↓↓I have the task of uploading pix from devices AND desktops, and could only find a javascript way of doing it. I ALMOST have it working - the parts I don't have working are how to pass in my other form fields (POST data) from my form. Here is the JS:
function fileSelected() { var count = document.getElementById('fileToUpload').files.length; document.getElementById('details').innerHTML = ""; for (var index = 0; index 1024 * 1024) fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB'; else fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB'; document.getElementById('details').innerHTML += 'Name: ' + file.name + 'Size: ' + fileSize + '
Type: ' + file.type; document.getElementById('details').innerHTML += '
'; } } function uploadFile() { var fd = new FormData(); var count = document.getElementById('fileToUpload').files.length; for (var index = 0; index
My form has a few fields, and an ID to make it workable by JS but I don't know how to combine my other POST fields in there to pass the data to my additem.php script. It's handling the upload great, and the php script is doing things like making 3 different sizes of the image, discarding the original huge image, checking to make sure it isn't a 'fake file type', etc.
But - I need to pass that $_POST data to it and my pure JS knowledge is spotty. I know I need to attach IDs to my form fields so JS can grab the values with GetElementById (super elementary stuff) but I'm not sure how to go from there. I hope someone can help me. I'm a 26 year code veteran who stupidly never made JavaScript her primary, first programming language.
Answer
Solution:
I see that you have the linevar fd = new FormData()
somewhere you will add the file to your formdata object using fd.append. You can this method to add your other fields as well
fd.append('username', 'Jhon');
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to undefined function str_contains()
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.