Im using ajax with javascript and data is processed on a php page with GET. Is it possible to send data from the php page when the GET query is empty? I want to do that because I have a live search that filters results but I want matching results to be displayed when users enters something in the search box and string matches it and when field is empty, I want all results to be displayed. So if a user enters something in the search box and get a matching result and then presses backspace to delete it, I want all results to appear. So obviously the best way would be to be able to do something on the php page when when the query is empty but when I do that it doesnt work. What would be the best way to accomplish this?
So the javascript page looks like this:
function showResults(str) {
if (str.length == 0) {
document.getElementById("results").innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET", "query.php?q=" + str, true);
xmlhttp.send();
}
}
Php page looks like the following:
$q = $_REQUEST["q"];
if ($q !== "") {
....Get Data from database or elsewhere here
echo $reponse;
}
If I do the following on the php page it doesnt work:
if ($q == "") {
....Get Data from database or elsewhere here
echo $reponse;
}
If I check with empty or with isset it also doesnt work propery.
Only place I can do something when search input is empty is on the javascript page
if (str.length == 0) {
document.getElementById("results").innerHTML = "";
return;
}
How can I make a query or other operation on the php page when search is empty? So I need ajax to send me all results from the database when field is empty.