简体   繁体   English

JQUERY 自动完成不显示从 php 脚本返回的结果

[英]JQUERY Autocomplete Not Showing Results That Are Returned From a php Script

I'm trying to fetch some school names using autocomplete.But the name list is not appearing.我正在尝试使用自动完成功能获取一些学校名称。但名称列表没有出现。 When I type something for example-" uni " it is making a request to sclist.php file like this sclist.php?term=uni and it returns this in response in the network console:当我输入例如“ uni ”时,它正在向 sclist.php 文件发出请求,例如这个sclist.php?term=uni并在网络控制台中返回此响应:

uni
 [
     "University of Cambridge",
     "University of Michigan",
     "University of Oxford",
     "University of Virginia" 
]

I can't seem to find why the list of names is not appearing.我似乎无法找到为什么名称列表没有出现。 This is my first autocomplete implementation.这是我的第一个自动完成实现。 Any help is much appreciated.任何帮助深表感谢。

the autocom.php file: autocom.php文件:

<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Autocomplete functionality</title>
       <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
       <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

      <!-- Javascript -->
      <script>
         $(function() {
            $( "#automplete" ).autocomplete({
               source: 'sclist.php',
               autoFocus:true
               });
         });
      </script>
   </head>

   <body>
      <!-- HTML -->
       <p>Type something</p>
         <input id = "automplete">
  </body>
</html>

the sclist.php file: sclist.php文件:

<?php
require_once('connectToDB.php');
if (isset($_GET['term'])) 
    echo $_GET['term']."\n";
else 
  echo "not set";

require_once 'connectToDB.php';
$stmt=$portbleDocObj->prepare('SELECT name FROM Institution WHERE name LIKE :prefix ');
$stmt->execute(array( ':prefix'=>$_GET['term']."%") );

$returedData=array();
while ( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) {
    $returedData[] = $row['name'];
}
print_r($row);
echo(json_encode($returedData, JSON_PRETTY_PRINT));
?>

Basically I have just done a bit of a tidy up on your original code.基本上,我刚刚对您的原始代码进行了一些整理。 Remember, everything you echo or print when you are returning to an AJAX call will go back to the AJAX code to process.请记住,当您返回到 AJAX 调用时,您回显或打印的所有内容都将 go 返回到 AJAX 代码进行处理。 So only return what should be returned.所以只返回应该返回的东西。 Also when talking machine to machine the PRETTY_PRINT is unnecessary此外,当机器与机器交谈时,不需要 PRETTY_PRINT

<?php

if ( ! isset($_GET['term'])) {
    echo "not set";
} else {
    require_once('connectToDB.php');
    $stmt = $portbleDocObj->prepare('SELECT name 
                                    FROM Institution 
                                    WHERE name LIKE :prefix');

    $stmt->execute( [':prefix' => $_GET['term']."%"] );
    $ret = [];
    while ( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) {
        $ret[] = $row['name'];
    }   
    echo json_encode($returedData);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM