GET request works:
var xhr = new XMLHttpRequest; xhr.onreadystatechange = function () { if (this.readyState == 4 && this.status == 200) { document.querySelector('.response').innerHTML = this.responseText; } } xhr.open("GET", "search.php?q="+val, true); xhr.send(); PHP (GET):
header("Content-type: text/plain; charset=windows-1251"); if(ISSET($_GET)) { $val = $_GET['q']; echo $val; } else { echo 'Not GET variables'; } POST:
var xhr = new XMLHttpRequest; xhr.onreadystatechange = function () { if (this.readyState == 4 && this.status == 200) { document.querySelector('.response').innerHTML = this.responseText; } } xhr.open("POST", "search.php", true); xhr.send(val); PHP:
header("Content-type: text/plain; charset=windows-1251"); if(ISSET($_POST)) { $val = $_POST['val']; echo $val; } else { echo 'Not POST variables'; } JS full:
var val; var inp = document.querySelector("#query-input"); inp.onkeyup = function () { val = inp.value; if (val.length != 0) { var xhr = new XMLHttpRequest; xhr.onreadystatechange = function () { if (this.readyState == 4 && this.status == 200) { document.querySelector('.response').innerHTML = this.responseText; } } xhr.open("POST", "search.php", true); xhr.send(val); } } POST request does not work. There is no answer at all. Those. there is a request, but the server is not responding, although the variable val is defined. How to make a request?