从Ajax发布到PHP时未定义的索引

Hoping someone can spot the issue here, I have been at it for hours.

I have a simple form

    <div class="ibox-content">

First name:<br>
<input type="text" name="firstname" id="firstname">
<br>
Last name:<br>
<input type="text" name="lastname" id="lastname">

<button class="btn btn-white" onclick="ajax_post();" type="submit">Test</button>
                       </div>

Which calls a very simple php file

<?php 
echo $_POST['first_name'];
echo $_POST['last_name'];
?>

Via an Ajax function

function ajax_post(){
// Create our XMLHttpRequest object
var hr = new XMLHttpRequest();
// Create some variables we need to send to our PHP file
var url = "result.php";
var fn = document.getElementById("firstname").value;
var ln = document.getElementById("lastname").value;
var vars = "first_name="+fn+"&last_name="+ln;
hr.open("POST", url, true);
// Set content type header information for sending url encoded variables in the request
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// 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("status").innerHTML = return_data;
    }
}
// Send the data to PHP now... and wait for response to update the status div
hr.send(vars); // Actually execute the request
document.getElementById("status").innerHTML = "processing...";
}

I keep getting "Undefined index" for each var back from the php file. Checking fiddler and wireshark, I can see the variables being posted.

For testing I gave the form a post action and I can see the php respond correctly.

Please help

If you try `var_dump($_POST)`, you will notice that the names of the form fields correspond with the `$_POST` array. so if you keep them the same, your code should work.

[edit]
Looking again at your JavaScript, it seems while you read the values of your input fields and concatenate them and store in your `vars` variable, you are not doing anything with them after that, especially you are not posting them to your PHP file.

Finally solved it, the issue was my url was defined as result.php ... I was using a htaccess though that looked after file extentions, changed the url to result and it worked. So many hours wasted!!!