I am trying to check if the text from textbox already exist in the database. The textbox use jquery autocomplete to select data from database as suggestions.
I want to prevent user input which is not already in the database.
Jquery for autocomplete:
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
$( function() {
$( "#school" ).autocomplete({
source: 'autocomplete.php'
}
change: function( event, ui ) {
if (ui.item == null) {
$("#school").val("");
$("#school").focus();
}
);
} );
</script>
PHP:
require_once('connect.php');
$term = $_GET['term'];
$stmt = $conn->prepare("SELECT aes_decrypt(SchoolList, 'MyString') AS SchoolList from Schools WHERE cast(aes_decrypt(SchoolList, 'MyString') as CHAR) LIKE :keyword");
$stmt->bindValue(':keyword', '%' . $term . '%');
$stmt->execute();
$result= array();
while($school = $stmt->fetch(PDO::FETCH_OBJ)){
array_push($result, $school->SchoolList);
}
echo json_encode($result);
HTML:
<label>What school does the child attend:</label>
<input type="search" name="school" id="school" style=" background: transparent;" spellcheck="false" required placeholder="Type School name and select from list" onblur="clearSelect()">
All the code above works perfectly
Below is my current code to check if textbox value exist. If the text does not exist, I want the script to clear the textbox to force the user to select from the autocomplete list, but I don't know how to get the variable to check from the php script):
$("#school").keyup(function{
if($("#school").val != ?????){
$("school").val == "";
}
});
}
Please help me to get the variable I need