1

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

  • Which _autocomplete_ plugin are you using? Is it jQuery-UI? – Phil Sep 20 '18 at 05:00
  • @Phil. I think so. I am still new to this, but I have edited my answer to include the jquery source files. – Charles Tester Sep 20 '18 at 05:10
  • Create a webservice for checking the text exists in db, and use an ajax call from keyup from the front end – Sumesh TG Sep 20 '18 at 05:10
  • See the [highest voted answer](https://stackoverflow.com/a/15704767/283366) – Phil Sep 20 '18 at 05:10
  • @Phil. Many thanks. As I said I am new to Ajax and Jquery. Where in my code would I insert the code for the answer? – Charles Tester Sep 20 '18 at 05:13
  • You would add the `change` function to the autocomplete config object, right next to `source`. I suggest you have a good read of the documentation ~ https://jqueryui.com/autocomplete/ – Phil Sep 20 '18 at 05:15
  • @Phil. I have edit my code as above and a few other ways but cannot get it to work. It the change function in the correct place? – Charles Tester Sep 20 '18 at 05:32
  • No, you need to move it up a line as a sibling to `source`, eg `.autocomplete({ source: 'autocomplete.php', change: function(event, ui) { ... }})` – Phil Sep 20 '18 at 05:33
  • Thanks. It worked. I wish I could vote up a comment. Many THANKS – Charles Tester Sep 20 '18 at 05:39
  • 1
    @CharlesTester go to the duplicate post and upvote the question and good answers instead – Phil Sep 20 '18 at 06:19

0 Answers0