Search code examples
phparrayssearch-enginesearch-suggestion

Show suggestions in a list one by one


How can I show suggestions in a list one by one?

When I run the below codes, I get this as my suggestion when I type in google: ["Google", "Gmail", "Google Maps"].

I want the suggestions to be shown in this way:

Google
Gmail
Google Maps

How can I do this?

If possible, please explain my mistakes.

Thanks in advance


Solution

  • You are returning json formatted objects in suggestion.php but using it as HTML in script_suggestion.php.

    Either update suggestion.php to return a HTML snippet or have the javascript in script_suggestion.php decode the json and use that to build the suggestions.

    Example HTML output:

    echo "<ul>";
    foreach($arr as $suggestion){
        echo "<li>".$suggestion."</li>";
    }
    echo "</ul>";