OneCompiler

Fetch suggestion

102
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Suggestions</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script> $(document).ready(function(){ $("#searchBox").keyup(function(){ var inputText = $(this).val(); $.ajax({ type: "POST", url: "get_suggestions.php", data: { input: inputText }, success: function(response){ $("#suggestionBox").html(response); } }); }); }); </script> </head> <body><input type="text" id="searchBox" placeholder="Type here"> <div id="suggestionBox"></div></body> </html> <?php $suggestions = array("apple", "banana", "orange", "mango", "grapes", "pineapple", "strawberry", "blueberry", "raspberry", "watermelon"); $input = $_POST['input']; $matches = array(); foreach ($suggestions as $suggestion) { if (strpos($suggestion, $input) !== false) { $matches[] = $suggestion; } } foreach ($matches as $match) { echo "<div>" . $match . "</div>"; } ?>