【问题标题】:auto-complete not working after taking input suggestions from php file从 php 文件中获取输入建议后自动完成不起作用
【发布时间】:2017-04-25 04:20:49
【问题描述】:

我想达到什么目标? 1. 建议列表(可能有相同的标签,但不同的描述) 2. 从该建议列表中选择一个项目时,另一个输入字段的输入应更改为所选项目的相应描述。

当我直接在 javascript 中初始化 var 项目时,它正在工作。但是当我将其更改为从 php 文件中获取建议列表时,只有它的选择部分正在工作,并且基于部分输入的过滤结果停止工作。

有关截图,请访问 github 上的此链接:- https://github.com/rohitdeepu17/BusinessManagement/tree/master/ProjectCode/TestingFiles/Screenshots

代码文件如下:- test_jquery_autocomplete.php

     <?php
          include 'session_check_common.php';
          include 'connect_my_sql_db.php';
     ?>

     <!doctype html>
     <html lang = "en">
     <head>
           <meta charset = "utf-8">
           <title>jQuery UI Autocomplete functionality</title>
           <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
     rel = "stylesheet">
           <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>

  <style>
     #project-label {
        display: block;
        font-weight: bold;
        margin-bottom: 1em;
     }
     #project-description {
        margin: 0;
        padding: 0;
     }
  </style>

  <!-- Javascript -->
  <script>
     $(function() {
        /*var projects = [
           {
              "label": "Java",
              "desc": "write once run anywhere"
           },
           {
              "label": "Java",
              "desc": "rohit here"
           },
           {
              "label": "jQuery UI",
              "desc": "the official user interface library for jQuery"
           },
           {
              "label": "Twitter Bootstrap",
              "desc": "popular front end frameworks "
           }
        ];*/
        $( "#project" ).autocomplete({
           minLength: 0,
           //source: projects,
           source: "get_customers.php",
           focus: function( event, ui ) {
              $( "#project" ).val( ui.item.label );
                 return false;
           },
           select: function( event, ui ) {
              $( "#project" ).val( ui.item.label );
              $( "#project-description" ).html( ui.item.desc );
              $( "#project-description" ).val( ui.item.desc );
              return false;
           }
        })

        .data( "ui-autocomplete" )._renderItem = function( ul, item ) {
           return $( "<li>" )
           .append( "<a>" + item.label + "<br>" + item.desc + "</a>" )
           .appendTo( ul );
        };
     });
  </script>
 </head>

  <body>
   <form action="test_autocomplete.php" class="subform" method="post">
  <div id = "project-label">Select a project (type "a" for a start):</div>
  <input id = "project">
  <input id = "project-description" name="projectdescription">

  <button type="submit">SUBMIT</button>
</form>

和 get_customers.php

  <?php 
    include 'connect_my_sql_db.php';
    $sql="select cust_id, cust_name, father_name from customer"; 
    $cust_name = array();
    $father_name = array();
    $result=mysqli_query($conn, $sql);
    while($row=mysqli_fetch_assoc($result)) 
    { 
      $title=$row['cust_name']; 
      $url=$row['father_name']; 
      $posts[] = array('label'=> $title, 'desc'=> $url);
    } 
    echo json_encode($posts);
   ?>

【问题讨论】:

    标签: javascript php jquery html sql


    【解决方案1】:

    你需要在cust_namelike的基础上过滤你的结果,

    $sql="SELECT cust_id, cust_name, father_name FROM customer 
            WHERE cust_name LIKE '%".$_REQUEST['term']."%'";
    

    另外,你需要初始化$posts变量,否则会导致未定义的错误,你的自动完成响应会失败,

    // add below line before while loop
    $posts = array();
    while($row=mysqli_fetch_assoc($result)) {
      ....
    

    【讨论】:

    • 我是 JQuery 新手。我不知道如何将“术语”传递给我的 php 文件。你能帮帮我吗?
    • 你不需要通过任期,在这里阅读更多jqueryui.com/autocomplete/#remote
    • @Roshan Kumar 为什么我更改 $posts[] = array('label'=> $title, 'desc'=> $url); to:- $posts[] = array('cust_name'=> $title, 'father_name'=> $url);然后相应地检索
    • 不要更改它们,否则您还需要在 JS 代码中更改它。参考stackoverflow.com/questions/11375922/…
    • 如果我想显示“customer_name 父亲姓名”之类的建议怎么办。但搜索条件应该只是 customer_name。只是为了让用户做出相应的选择,而不是一遍又一遍地检查他想选择哪一个。
    猜你喜欢
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 2020-08-21
    • 1970-01-01
    • 2015-10-19
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多