Не могу найти ошибку... После клика по кнопе ничего не происходит...
<form id="form-name">
<input type="text" name="name" id="text-field">
<input type="text" name="response" id="response-field">
<button type="submit" id="btn">Click me</button>
</form>
window.onload = function() {
var form = document.getElementById('form-name');
var textField = document.getElementById('text-field');
form.onsubmit = function() {
var request = new XMLHttpRequest();
var text = textField.value;
request.open('POST', 'script.php', true);
request.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 200) {
document.getElementById('response-field').value = this.responseText;
// console.log(this.responseText);
}
};
var date = {name: text};
request.setRequestHeader('Content-type', 'application/json');
request.send(JSON.stringify(date));
return false;
};
};
if (isset($_POST['name'])) {echo json_encode($_POST['name']);}