Core JavaScript

Get Search Suggestions in PHP via AJAX

AJAX is a method for retrieving data from a server without refreshing the web page. There are two files below which you can create with a text editor. These files are assumed to be in the same folder on the server.

SearchSuggestions.html

<html>
	<head>
		<title>XoaX.net's Javascript AJAX Example</title>
		<script>
			function FindSuggestions(str) {
				if (str.length == 0) {
					document.getElementById("idSuggestions").innerHTML = "";
					return;
				} else {
					var xmlhttp = new XMLHttpRequest();
					xmlhttp.onreadystatechange = function() {
						if (this.readyState == 4 && this.status == 200) {
							document.getElementById("idSuggestions").innerHTML = this.responseText;
						}
					};
					xmlhttp.open("GET", "SearchSuggestions.php?sSearch=" + str, true);
					xmlhttp.send();
				}
			}
		</script>
	</head>
	<body>
		<form action="">
			<fieldset>
				<legend><b>Type here to get a suggestion:</b></legend>
				<label for="fname">Search:</label>
				<input type="text" id="idSearch" name="nSearch" onkeyup="FindSuggestions(this.value)">
			</fieldset>
		</form>
		<p>Suggestions: <span id="idSuggestions"></span></p>
	</body>
</html>

SearchSuggestions.php

<?php

$saTerms[] = "Aaron";
$saTerms[] = "Abel";
$saTerms[] = "Abigail";
$saTerms[] = "Abner";
$saTerms[] = "Abraham";
$saTerms[] = "Ada";
$saTerms[] = "Adam";
$saTerms[] = "Ananias";
$saTerms[] = "Andrew";
$saTerms[] = "Anna";
$saTerms[] = "Baal";
$saTerms[] = "Barak";
$saTerms[] = "Barnabas";
$saTerms[] = "Benjamin";
$saTerms[] = "Boaz";
$saTerms[] = "Cain";
$saTerms[] = "Caleb";
$saTerms[] = "Canaan";
$saTerms[] = "Candace";
$saTerms[] = "Cephas";
$saTerms[] = "Chloe";
$saTerms[] = "Claudia";
$saTerms[] = "Cyrus";
$saTerms[] = "Dan";
$saTerms[] = "David";
$saTerms[] = "Delilah";
$saTerms[] = "Dinah";
$saTerms[] = "Edom";
$saTerms[] = "Eli";
$saTerms[] = "Elijah";
$saTerms[] = "Elisha";
$saTerms[] = "Elizabeth";
$saTerms[] = "Elon";
$saTerms[] = "Emmanuel";
$saTerms[] = "Ephraim";
$saTerms[] = "Esau";
$saTerms[] = "Esther";
$saTerms[] = "Eve";
$saTerms[] = "Ezekiel";
$saTerms[] = "Ezra";
$saTerms[] = "Felix";
$saTerms[] = "Festus";
$saTerms[] = "Gabriel";
$saTerms[] = "Gad";
$saTerms[] = "Gideon";
$saTerms[] = "Goliath";
$saTerms[] = "Gomer";
$saTerms[] = "Habakkuk";
$saTerms[] = "Hadassah";
$saTerms[] = "Hagar";
$saTerms[] = "Haggai";
$saTerms[] = "Ham";
$saTerms[] = "Herod";
$saTerms[] = "Hezekiah";
$saTerms[] = "Hosanna";
$saTerms[] = "Hosea";
$saTerms[] = "Ichabod";
$saTerms[] = "Isaac";
$saTerms[] = "Isaiah";
$saTerms[] = "Ishmael";
$saTerms[] = "Israel";
$saTerms[] = "Jacob";
$saTerms[] = "James";
$saTerms[] = "Jeremiah";
$saTerms[] = "Jeroboam";
$saTerms[] = "Jesse";
$saTerms[] = "Jesus";
$saTerms[] = "Jezebel";
$saTerms[] = "Job";
$saTerms[] = "Joel";
$saTerms[] = "John";
$saTerms[] = "Jonah";
$saTerms[] = "Joseph";
$saTerms[] = "Joshua";
$saTerms[] = "Judah";
$saTerms[] = "Judas";
$saTerms[] = "Judith";
$saTerms[] = "Keziah";
$saTerms[] = "Laban";
$saTerms[] = "Leah";
$saTerms[] = "Levi";
$saTerms[] = "Leviathan";
$saTerms[] = "Lot";
$saTerms[] = "Luke";
$saTerms[] = "Malachi";
$saTerms[] = "Manasseh";
$saTerms[] = "Mark";
$saTerms[] = "Martha";
$saTerms[] = "Mary";
$saTerms[] = "Matthew";
$saTerms[] = "Matthias";
$saTerms[] = "Medad";
$saTerms[] = "Meshach";
$saTerms[] = "Micah";
$saTerms[] = "Michael";
$saTerms[] = "Moab";
$saTerms[] = "Moses";
$saTerms[] = "Nahor";
$saTerms[] = "Nahum";
$saTerms[] = "Naomi";
$saTerms[] = "Nathan";
$saTerms[] = "Naphtali";
$saTerms[] = "Nebuchadnezzar";
$saTerms[] = "Nehemiah";
$saTerms[] = "Nicodemus";
$saTerms[] = "Nimrod";
$saTerms[] = "Noah";
$saTerms[] = "Obadiah";
$saTerms[] = "Obed";
$saTerms[] = "Onesimus";
$saTerms[] = "Onias";
$saTerms[] = "Paul";
$saTerms[] = "Peter";
$saTerms[] = "Philemon";
$saTerms[] = "Philip";
$saTerms[] = "Rachel";
$saTerms[] = "Raphael";
$saTerms[] = "Rehoboam";
$saTerms[] = "Reuben";
$saTerms[] = "Ruth";
$saTerms[] = "Salome";
$saTerms[] = "Samson";
$saTerms[] = "Samuel";
$saTerms[] = "Sarah";
$saTerms[] = "Saul";
$saTerms[] = "Seth";
$saTerms[] = "Shem";
$saTerms[] = "Simon";
$saTerms[] = "Solomon";
$saTerms[] = "Stephen";
$saTerms[] = "Terah";
$saTerms[] = "Theophilus";
$saTerms[] = "Thomas";
$saTerms[] = "Timothy";
$saTerms[] = "Titus";
$saTerms[] = "Tobias";
$saTerms[] = "Tobit";
$saTerms[] = "Uriah";
$saTerms[] = "Uriel";
$saTerms[] = "Uzziah";
$saTerms[] = "Vashti";
$saTerms[] = "Veronica";
$saTerms[] = "William";
$saTerms[] = "Wolfgang";
$saTerms[] = "Yocheved";
$saTerms[] = "Yotam";
$saTerms[] = "Zacchaeus";
$saTerms[] = "Zebadiah";
$saTerms[] = "Zechariah";
$saTerms[] = "Zephaniah";
$saTerms[] = "Zion";

$sSearch = $_REQUEST["sSearch"];

$sSuggestion = "";

// Check for a search sequence before for checking for suggestions.
if ($sSearch !== "") {
  $sSearch = strtolower($sSearch);
  $sSearchLength = strlen($sSearch);
  foreach($saTerms as $sTerm) {
  	// Check whether the beginning of the term matches the search term
    if (stristr($sSearch, substr($sTerm, 0, $sSearchLength))) {
      if ($sSuggestion === "") {
        $sSuggestion = $sTerm;
      } else {
        $sSuggestion .= ", $sTerm";
      }
    }
  }
}

// Out the suggestions that will be in the returned string
echo (($sSuggestion === "") ? "Nothing was found" : $sSuggestion);

?>
 
 

Output

 
 

© 2007–2025 XoaX.net LLC. All rights reserved.