0

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.

1
  • WOuld it be in here somewhere? for (var index = 0; index < count; index ++) { var file = document.getElementById('fileToUpload').files[index]; fd.append('myFile', file); //like this: fd.append('item',post); // I just seriously don't know how to write it } Commented Aug 15, 2021 at 17:26

1 Answer 1

0

I see that you have the line var 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');
Sign up to request clarification or add additional context in comments.

2 Comments

so how do I get the value? assign it to a var? (kicking self once again for not learning pure JavaScript about 20 years ago)...
I got it working. Only one hurdle left! I was able to use fd.append and got the values from document.GetElementById('fieldname').value; and pass it to the PHP script. Throwing confetti...but now - my php script runs and I want it to resolve to a page. I use header("Location..."); in php but it isn't doing that with this JavaScript Twist; instead, it's opening a small window with a bunch of my html in it. Scratching head. Once I can get past this annoyance, things are golden.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.