我想使用php和html进行文本自动完成。
我试过下面的代码

<?php
$connection = mysqli_connect("localhost", "root", "pass", "data") or die("Error " . mysqli_error($connection));
$sql = "select value from fin";
$result = mysqli_query($connection, $sql) or die("Error " . mysqli_error($connection));
$dna = array();

while ($row = mysqli_fetch_array($result))
    {
    $dna[] = $row['value'];
    }

$jj = array_unique($dna);
print_r(array_values($jj));
?>


结果是javascript - 使用php和html自动完成-LMLPHP

我的HTML

<head>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/
            themes/smoothness/jquery-ui.css">
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="http://code.jquery.com/ui/1.10.4
         /jquery-ui.js">
    </script>
</head>

<body>
    <form name="vinform" method="get"> <input type="text" name="editor" autocomplete="on"> <input type="submit" value="Show" id="display"> </form>
    <div id="div1"></div>
    <script type="text/javascript">
    $(function() {
        $('#div1').autocomplete({
            source: "auto.php"
        });
    });
    </script>
</body>


当我在文本字段中键入某些单词时,它不会显示mysql中的单词。.当我在文本字段中键入字符时,我必须根据输入的文本字段显示来自mysql的相关单词。我要解决我代码中的问题?

尝试与Ajax

              var se = null;

          $(function () {
          var minlength = 1;

       $("#editor").keyup(function () {
        var that = this,
    value = $(this).val();

    if (value.length >= minlength ) {
        if (se != null)
            se.abort();
        se = $.ajax({
            type: "GET",
            url: "auto.php",
            data: value,
            dataType: "text",
            success: function(msg){

                if (value==$(that).val()) {

                }
                   }
                });
               }
             });
          });


的PHP

                    <?php

                if(isset($_GET['editor']))
                {
                $con=mysqli_connect("localhost","root","admin321","data");

                if (mysqli_connect_errno())
                  {
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
                  }
                  $name=$_GET['editor'];
        $sql = "select value from fin where value LIKE '%".$name."'";
          $result = mysqli_query($connection, $sql) or
           die("Error " . mysqli_error($connection));

            $dna = array();
            while($row = mysqli_fetch_array($result))
            {
                $dna[] = $row['value'];
            }
            $jj=array_unique($dna);
             print_r ( $jj);

                }
        ?>


没有自动完成动作

最佳答案

使用选项1(Jquery UI自动完成),然后尝试类似的操作

<?php
$connection = mysqli_connect("localhost", "root", "pass", "data") or die("Error " . mysqli_error($connection));
$sql = "select value from fin";
$result = mysqli_query($connection, $sql) or die("Error " . mysqli_error($connection));
$dna = array();

while ($row = mysqli_fetch_array($result))
    {
    $dna[] = $row['value'];
    }

echo json_encode($dna);
?>


jQuery UI autocomplete关于源选项的状态


  字符串:使用字符串时,自动完成插件会期望该字符串指向将返回JSON数据的URL资源。它可以在同一主机上,也可以在其他主机上(必须提供JSONP)。 Autocomplete插件不会过滤结果,而是会在查询字符串中添加一个term字段,服务器端脚本应使用该字段来过滤结果。例如,如果将source选项设置为“ http://example.com”,并且用户键入foo,则将对http://example.com?term=foo进行GET请求。数据本身可以采用与上述本地数据相同的格式。

10-05 20:29
查看更多