Skip to content
Advertisement

Why is php POST array empty for me in one instance and populated in another, both using XHR POST?

I am trying to write code that will check if username is already used, and I wrote some test code where my php $_POST array was populated. However, I modified the test code slightly for the ‘production’ code I am developing, and my $_POST array is empty. My first set of code, that is working, html file is

JavaScript

First set of code javascript file

JavaScript

First set of code php file

JavaScript

My modified code that does not work is below. The html file is

JavaScript

The javascript file for the code that does not work is

JavaScript

and finally my php code that does not work is

JavaScript

Not sure where I have gone wrong in the second set of code. I have tried putting my data in a FormData object and passing that to the XHR.send() method. I realize I have switched in my first set of code from a ‘blur’ event on the input I am interested in directly, to my second set of code where my main event is clicking on a ‘checkAvailability’ button, but I can’t see where that would make my $_POST array not accept the sent value. In both cases I can see where in the request there is username=nameEntryFromInputTextBox. Please take a look and advise.

Advertisement

Answer

I had a typo, in my code that was not working I had Content-type set to x-www-url-formencoded. It should have been x-www-form-urlencoded.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement