23

This is my javascript

function ajax_post(){
            // Create our XMLHttpRequest object
            var hr = new XMLHttpRequest();
            // Create some variables we need to send to our PHP file
            var url = "LiveUpdate.php";
            var sb = document.getElementById("LiveUpdate").value;
            var FirstName = document.getElementById("FirstName").value;
            var images = document.getElementById("images").value;

            var vars = "update="+sb+"&FirstName="+FirstName+"&images="+images;
            hr.open("POST", url, true);
            // Set content type header information for sending url encoded variables in the request
            hr.setRequestHeader("Content-type", "multipart/form-data");
            // Access the onreadystatechange event for the XMLHttpRequest object
            hr.onreadystatechange = function() {
                if(hr.readyState == 4 && hr.status == 200) {
                    var return_data = hr.responseText;
                    document.getElementById("message").innerHTML = return_data;
                }
            }
            // Send the data to PHP now... and wait for response to update the status div
            var formData = new FormData(document.querySelector("form"));
            hr.send(formData); // Actually execute the request // Actually execute the request
            document.getElementById("message").innerHTML = "processing...";
        }
            //show image before uploading
                var loadFile = function(event) {
                var output = document.getElementById('output');
                output.src = URL.createObjectURL(event.target.files[0]);
                };//end image

and this is the form. javascript and the form is in the same file. the file name is sample.php

<form action="popup.php" method="post" id="myForm" name="myForm" enctype="multipart/form-data">
    <div id="message"></div>
    <img src="" id="output" />
    <input type="file" class="filestyle" data-buttonBefore="true" accept=".png, .jpg, .jpeg" onchange="loadFile(event)" name="images" id="images">
    <input class="form-control" type="text" placeholder="First Name" name="FirstName" id="FirstName" value="">
    <a href="#" class="btn btn-success" role="button" name="update" id="LiveUpdate"  onclick="javascript:ajax_post();">Update</a>
</form>

the idea in this code is. i want to insert the basic information like FirstName and Image into the database live without refreshing the page. the reason why i choose tag to submit the ajax. so the page url Account.php?Edit=1 will not change to Account.php because if it change to account.php the popup editing mode will be close. the problem in this code is. i don't know how to fix the error Warning: Missing boundary in multipart/form-data POST data in Unknown on line 0 can anyone help me how to fix this. thank you!

Vanquished Wombat
  • 9,075
  • 5
  • 28
  • 67
Kenneth Suaverdez
  • 331
  • 1
  • 2
  • 9
  • Possible duplicate of [Warning: Missing boundary in multipart/form-data POST data in Unknown on line 0](http://stackoverflow.com/questions/27635738/warning-missing-boundary-in-multipart-form-data-post-data-in-unknown-on-line-0) – Vanquished Wombat Nov 12 '16 at 10:11

3 Answers3

44

The browser sets the correct headers (including the correct multipart boundary indication in the Content-type) if you haven't manually specified anything.

So all you need to do is remove the following line:

 hr.setRequestHeader("Content-type", "multipart/form-data");

Otherwise you will need to set the boundary yourself as it is suggested by Ellias Van Ootegem in Uploading a file with XMLHttprequest - Missing boundary in multipart/form-data:

hr.setRequestHeader("Content-type","multipart/form-data; charset=utf-8; boundary=" + Math.random().toString().substr(2));
Community
  • 1
  • 1
Suat Hyusein
  • 495
  • 3
  • 14
2

Just remove the setRequestHeader()

barbachose
  • 21
  • 1
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 22 '21 at 05:59
0

This works as well. Taken from https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type

const config = { headers: { Authorization: Bearer ${localStorage.token}, "Content-Type": "multipart/form-data; boundary=---------------------------974767299852498929531610575" } };

Steven
  • 1,071
  • 15
  • 15