0

I cannot seem to send any POST data to a PHP file via XMLHttpRequest. I have read many questions like this one but everyone had a different issue - none of them seem to be the case here.

I have boiled these two files down to their absolute core basics and it is still is not receiving any POST data. I have done this the exact same way in plenty of other instances before and I'm not sure how this one is any different.

index.php

...
<button id="login-button">Log in</button>
...

Javascript:

function login() {
  let ajax = new XMLHttpRequest();
  ajax.open('POST', 'login.php', true);
  ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
  ajax.onload = function() {
      alert(this.response);
  };
  ajax.send({user:'hello', password:'there'});
}
document.getElementById('login-button').addEventListener('click', login)

login.php:

var_dump($_POST);

The alert message with the output, every single time, simply reads:

array(0) {
}

The JS and PHP are both in the same folder of the same website on the same server, running PHP 7 if that matters. What could I possibly be doing wrong here?

9
  • 1
    Have you opened the browser's developer tools and had a look at the network tab? Commented Apr 29, 2020 at 14:52
  • 1
    Try to change the send function to ajax.send("user=hello&password=there") and see what you get Commented Apr 29, 2020 at 14:52
  • ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); tells the server to expect url-encoded data, but then you've sent something else (which I assume is maybe an attempt to send JSON data, but isn't valid as such). Commented Apr 29, 2020 at 14:57
  • How are you capturing the button click event? Commented Apr 29, 2020 at 15:06
  • @JayBlanchard Please see the above Javascript. Commented Apr 29, 2020 at 15:18

1 Answer 1

2

By using ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); you basically tell your request to expect url-encoded data.

Lets keep it very simple, you want to submit a username and password.

So the request should look like this ajax.send("username=hello&password=there")

In your sample code you tried to send I dont know what kind of object-notation. The go-to way to exchange data between frontend and backend is JSON.

To modify your example to work with json modify it in the following way:

ajax.setRequestHeader("Content-Type", "application/json");
let data = JSON.stringify({"username": "hello", "password": "there"});
ajax.send(data);

To get an object out of a valid JSON string you can use the json parse method pe this helps you out :)

Sign up to request clarification or add additional context in comments.

1 Comment

Note that the PHP side has to be changed too, in order to read incoming JSON data - see stackoverflow.com/questions/18866571/receive-json-post-with-php

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.