Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XMLHttpRequest & FormData not submitting data

I am trying to submit a form via ajax using the post method and a FormData object.

Here is a simplified version of the JavaScript:

var form=…; //  form element
var url=…;  //  action
form['update'].onclick=function(event) {    //  button name="update"
    var xhr=new XMLHttpRequest();
        xhr.open('post',url,true);
        xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    var formData=new FormData(form);
        formData.append('update', true);    // makes no difference
    xhr.send(formData);
    xhr.onload=function() {
        alert(this.response);
    };
};

The form has:

  • a button (type="button" name="update") to run the script
  • no action and method="get"

My PHP script has the following:

if(isset($_POST['update'])) {
    print_r($_POST);
    exit;
}

//  more stuff

print 'other stuff';

When I try it, the PHP falls through to the rest of the code, and I get the other output, rather than what I expect from the print_r statement.

I have tried the following variations:

  • new FormData() (without the form). This does work if I add the update data manually.
  • new FormData(form). This does not work, whether I add the update manually or not.
  • changing the form method to post.
  • Firefox, Safari & Chrome on MacOS; all current versions.

The from itself looks something like this:

<form id="edit" method="post" action="">
    <p><label for="edit-summary">Summary</label><input id="edit-summary" name="summary" type="text"></p>
    <p><label for="edit-description">Description</label><input id="edit-description" name="description" type="text"></p>
    <p><label for="edit-ref">Reference</label><input id="edit-ref" name="ref" type="text"></p>
    <p><label for="edit-location">Location</label><input id="edit-location" name="location" type="text"></p>
    <p><button type="button" name="update">OK</button></p>
</form>

What should I do to submit the get this to work?

No jQuery, please.

like image 479
Manngo Avatar asked Jul 18 '17 02:07

Manngo


People also ask

What is XMLHttpRequest used for?

XMLHttpRequest (XHR) objects are used to interact with servers. You can retrieve data from a URL without having to do a full page refresh. This enables a Web page to update just part of a page without disrupting what the user is doing. XMLHttpRequest is used heavily in AJAX programming.

Is XMLHttpRequest same as AJAX?

XHR is the XMLHttpRequest Object which interacts with the server. Ajax technique in the nutshell leverages the XHR request to send and receive data from the webserver. This object is provided by the browser's javascript environment. It transfers the data between the web browser and server.

Is XMLHttpRequest an API?

XMLHttpRequest (XHR) is a JavaScript API to create AJAX requests. Its methods provide the ability to send network requests between the browser and a server.

Is XMLHttpRequest better than fetch?

The Fetch API makes it easier to make asynchronous requests and handle responses better than using an XMLHttpRequest . Fetch allows us to create a better API for the simple things, using modern JavaScript features like promises . Let's start fetching !!


1 Answers

The content type when sending a FormData object is multipart/form-data not url encoded.
Further more the proper boundary must be set for the request, which the user is unable to do. For this XMLHttpRequest sets the correct content type with the required boundary.
So all you have to do is not set the content type and it'll work.

var xhr=new XMLHttpRequest();
xhr.open('post',url,true);
//xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");<--don't do this
var formData=new FormData(form);
formData.append('update', true);    // makes no difference
xhr.send(formData);
xhr.onload=function() {
    alert(this.response);
};
like image 152
Musa Avatar answered Oct 07 '22 14:10

Musa