doutou7286 2015-12-14 14:05
浏览 34
已采纳

用于jQuery UI自动完成的简单PHP脚本(不含MySQL)

I have the following HTML with jQuery:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Test</title>
    <script type="text/javascript" src="../scripts/jquery-2.1.4.min.js"></script>
    <link href="../scripts/ui/jquery-ui.min.css" rel="stylesheet" type="text/css">
    <script type="text/javascript" src="../scripts/ui/jquery-ui.min.js"></script>
    <script type="text/javascript">
        $(function() {
            $("#acInput").autocomplete({
                source: "autocomplete.php"
            })
       })
    </script>
</head>
<body>
    <div class="ui-widget">
        <label for="acInput">Flower Name: </label><input type="text" name="acInput" id="acInput">
    </div>
</body>
</html>

and I want to create a PHP file (autocomplete.php which would be in the same directory) that will search through array and return response:

I've tried the following way:

<?php
$flowers = ["Aster", "Daffodil", "Rose","Peony", "Primula", "Snowdrop", "Poppy", "Primrose", "Petuna", "Pansy"];
$matches = [];
if (isset($_GET["term"])) {  
    $term = trim($_GET["term"]);  // do I need to remove/strip tags or escape it and what is the best way?
    if (!empty($term)) {
        $pattern = '/^'+$term+'/';
        foreach ($flowers as $cvet) {
            preg_match($pattern, $cvet, $matches);
        }
        echo json_encode($matches);
    }
}

But it doesn't work. I assume that the problem has to do with $pattern or preg_match, since this is the first time I use regular expression with PHP?

EDIT: What I want is to check:

1. Are there values in the array $flowers that are equal to $term OR start with $term;

2. Are there values in the array $flowers containing $term.

Return all of these $flowers array values (elements) to jQuery autocomplete and display them as suggestions. Thanks!

  • 写回答

2条回答 默认 最新

  • drv16821 2015-12-14 14:10
    关注

    I would rather give you a much simplified version, without using regex:

    <?php
      $flowers = ["Aster", "Daffodil", "Rose","Peony", "Primula", "Snowdrop", "Poppy", "Primrose", "Petuna", "Pansy"];
    
      // If the `term` is set.
      if (isset($_GET["term"])) {
        $flowers = array_filter($flowers, "filter_out");
      }
    
      // Callback function for the array to filter only those values contain the term.
      function filter_out ($var) {
        return strpos($var, $_GET["term"]) !== false;
      }
    
      // encode and send it back.
      echo json_encode($flowers);
    

    Output

    Request: autocomplete.php
    Response

    ["Aster","Daffodil","Rose","Peony","Primula","Snowdrop","Poppy","Primrose","Petuna","Pansy"]
    

    Request: autocomplete.php?term=p
    Response:

    ["Snowdrop","Poppy"]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用