dsjhejw3232 2016-10-30 19:44
浏览 23
已采纳

Ajax响应空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.

  • 写回答

2条回答 默认 最新

  • dpf56454 2016-10-30 20:08
    关注

    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);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?