xhttp.onreadystatechange = function () {
alert(xhttp.readyState+", "+xhttp.status);
};
It displays, three alerts:
1,0
2,0
4,0
and in the network tab of the browser, I see 200 OK, and the form info is being submitted correctly.
What I understand is that when the readyState is 4, the status is still 0, hence my IF statement (in OP) doesn’t trigger. So, any ideas what to do to solve this?
I am testing in Firefox. I can’t share the whole file but basically it sends the form info via mail() as follows:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Get form data
...
// Validate form data
if (...) {
...
mail(...);
}
}
?>
The form info is sent with no issues, just the xhttp.status is not returning as 200 as it should.
If you have any working code samples that does the job (sending form info via XMLHttpRequest), or if you could direct to any source, that might help me figure out what I am doing wrong. I just grabbed the code I found on W3Schools, which is not working in my case.
Sorry, I forgot to mention. The script I am posting is on another domain. Still, the script is doing its job and in Firefox Network tab I am seeing 200 OK. So, I don’t understand why the xhttp.status keeps returning as 0. Shall I just ignore it?
Ah, probably a cross-origin problem then… I’m surprised you’re not getting an error about that in the console. You’ll probably need to configure your PHP script to send the relevant CORS headers.