When this function is run, the request.status = 0. This usually means that the network is down but since the webpage loaded, this cannot be possible. I think it may be because I am using XAMPP and running on a localhost but I am not sure how to fix the problem.
Here is the javascript function:
<script type="text/javascript">
function submitForm() {
var userKey;
var username = login_form.elements["username"].value;
function getKey() {
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if(request.readyState == 4 && request.status == 200){
userKey = request.responseText;
}
}
request.open("POST", "key.php", true);
request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
request.send("username="+username);
}
getKey();
}
</script>
Here is the php function called "key.php". It is in the same folder as the html file:
<?php
if (array_key_exists('username', $_POST)) {
$username = $_POST['username'];
echo '<script language="javascript">';
echo 'alert("message successfully sent")';
echo '</script>';
} else {
echo 'Invalid parameters!';
}
?>