如何从php json获取jquery变量

时间:2018-09-20 04:49:09

标签: javascript php jquery

我正在尝试检查文本框中的文本是否已存在于数据库中。文本框使用jquery自动完成功能从数据库中选择数据作为建议。

我要阻止数据库中尚未存在的用户输入。

自动完成的jQuery

<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()">

上面的所有代码都能完美地工作

下面是我当前的代码,用于检查文本框值是否存在。如果文本不存在,我希望脚本清除文本框以强制用户从自动完成列表中进行选择,但是我不知道如何从php脚本中获取要检查的变量):

 $("#school").keyup(function{
    if($("#school").val != ?????){
        $("school").val == "";
    }
});

}

请帮助我获取所需的变量

0 个答案:

没有答案