<?php
	$db = new mysqli('localhost', 'root' ,'', 'capstone');
	
	if(!$db) {
	
		echo 'Could not connect to the database.';
	} else {
	

		if(isset($_POST['queryString'])) {
			$queryString = $db->real_escape_string($_POST['queryString']);
			
			if(strlen($queryString) >0) {

				$query = $db->query("SELECT firstname, lastname FROM guards WHERE firstname LIKE '$queryString%' LIMIT 10");
				if($query) {
				echo '<ul class="clickme">';
					while ($result = $query ->fetch_object()) {
	         			echo '<li>'.$result->firstname.' '.$result->lastname.'</li>';
	         		}
				echo '</ul>';
					
				} else {
					echo 'OOPS we had a problem :(';
				}
			} else {
				// do nothing
			}
		} else {
			echo 'There should be no direct access to this script!';
		}
	}	
?>

<script>
$(document).ready( function() {
	$('.clickme li').click( function() {
		var valueme = $(this).html();
		$(".guards").val(valueme);
	})
})
</script>