coder_1432 coder_1432 - 1 month ago 11
PHP Question

Ajax response empty GET

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.

Nvj Nvj
Answer

Maybe I'm going a bit off-topic here, but I would like to suggest a completely different technique for your particular case.

Here goes: At the time of initial page load, download all search results on the web page. When the user enters some text in the search box, just parse the search results on the web page at client side using JavaScript. When the user deletes the entered text, again use JavaScript to display all the search results. This way, you don't have to send HTTP requests for each "input" by the user, thus increasing the response time between the user entering some text and getting the related search results.

Again, my answer may be off-topic, but I implemented this a while ago and the results were amazing compared to sending GET/POST requests again and again.

Send JSON from PHP to JavaScript on page load:

<?php

    function get_all_search_results()
    {
        // Database query to get all search results in an array
        $search_results_array = "";

        // Convert the array to a JSON object
        $json = json_encode($search_results_array);

        // Echo it
        echo $json;
    }

?>

At page load, call the above function by an onload JavaScript function like this:
// JSON object var searchResultsArray;

    function getAllSearchResults()
    {
        // An ajax request to get all search results in a variable
        var allresults = // result of the ajax request

        // JSON decode the results
        searchResultsArray = JSON.parse(allresults);
    }
Comments