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

用于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!

我有以下带有jQuery的HTML: p>

 &lt;  !DOCTYPE html&gt; 
&lt; html&gt; 
&lt; head&gt; 
&lt; meta charset =“UTF-8”&gt; 
&lt; title&gt;测试&lt; / title&gt; 
&lt; script type =“text / javascript  “src =”../ scripts / jquery-2.1.4.min.js“&gt;&lt; / script&gt; 
&lt; link href =”../ scripts / ui / jquery-ui.min.css“rel  =“stylesheet”type =“text / css”&gt; 
&lt; script type =“text / javascript”src =“../ scripts / ui / jquery-ui.min.js”&gt;&lt; / script&gt;  
&lt; script type =“text / javascript”&gt; 
 $(function(){
 $(“#acInput”)。autocomplete({
 source:“autocomplete.php”
})
  })
&lt; / script&gt; 
&lt; / head&gt; 
&lt; body&gt; 
&lt; div class =“ui-widget”&gt; 
&lt; label for =“acInput”&gt;花名:&lt;  ; / label&gt;&lt; input type =“text”name =“acInput”id =“acInput”&gt; 
&lt; / div&gt; 
&lt; / body&gt; 
&lt; / html&gt; 
  code>  
 
 

我想创建一个PHP文件(autocomp lete.php将在同一目录中)搜索数组并返回响应: p>

我尝试了以下方法: p>

  &lt;?php 
 $ flowers = [“Aster”,“Daffodil”,“Rose”,“Peony”,“Primula”,“Snowdrop”,“Poppy”,“Primrose”,“Petuna”,“  Pansy“]; 
 $ matches = []; 
if(isset($ _ GET [”term“])){
 $ term = trim($ _ GET [”term“]);  //我需要删除/去除标签还是逃避它以及最好的方法是什么?
 if(!empty($ term)){
 $ pattern ='/ ^'+ $ term +'/'; 
  foreach($ flowers as $ cvet){
 preg_match($ pattern,$ cvet,$ matches); 
} 
 echo json_encode($ matches); 
} 
} 
  code>  pre  > 
 
 

但它不起作用。 我认为问题与 $ pattern em>或 preg_match em>有关,因为这是我第一次使用PHP的正则表达式? p> 编辑: strong>我想要检查: p>

1。 strong>数组$ flowers中的值是否等于 $ term或以$ term开头; p>

2。 strong>数组$ flowers中是否包含$ term的值。 p>

将所有这些$ flowers数组值(元素)返回到jQuery自动完成并将其显示为建议。 谢谢! p> div>

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"]

<?php

$flowers = ["aster", "daffodil", "rose","peony", "primula", "snowdrop", "poppy", "primrose", "petuna", "pansy"];

$matches = [];

if (isset($_GET["term"]) && !empty($_GET["term"])) {  
    $term = trim( strip_tags($_GET["term"]) );  
    if (!empty($term)) {
        $pattern = '/^'. strtolower($term) .'/ui';
        foreach ($flowers as $cvet) {
            preg_match($pattern, $cvet, $m);
            if(isset($m[0])) $matches[] = $m[0]; // or ucfirst($m[0]) (if you want)
        }
        echo json_encode($matches);
    }
}