简体   繁体   English

从php文件中获取输入建议后,自动完成功能不起作用

[英]auto-complete not working after taking input suggestions from php file

What I want to achieve? 我想实现什么? 1. a list of suggestions (may have same label, but different desc) 2. on selecting one item from that from that list of suggestions, another input field's input should change to the corresponding desc of the selected item. 1.建议列表(可能具有相同的标签,但描述不同)2.从建议列表中选择一项时,另一输入字段的输入应更改为所选项目的相应desc。

When I was initializing var projects directly in javascript, it was working. 当我直接使用javascript初始化var项目时,它正在工作。 But when I changed it to take list of suggestions from a php file, only it's selection part is working and filtering results on the basis of partial input stopped working. 但是,当我更改它以从php文件中获取建议列表时,只有它的选择部分有效,并且基于部分输入的过滤结果已停止工作。

For screenshots, please visit this link on github :- https://github.com/rohitdeepu17/BusinessManagement/tree/master/ProjectCode/TestingFiles/Screenshots 有关屏幕截图,请访问github上的此链接:-https: //github.com/rohitdeepu17/BusinessManagement/tree/master/ProjectCode/TestingFiles/Screenshots

and the code files are as below:- test_jquery_autocomplete.php 和代码文件如下:-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>

and get_customers.php 和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);
   ?>

You need to filter your result on the basis of cust_name like, 您需要根据cust_name过滤结果,例如,

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

Also, you need to initialise $posts variable, otherwise it will cause error of undefined and your Autocomplete response will fails, 另外,您需要初始化$posts变量,否则将导致未定义的错误,并且您的自动完成响应将失败,

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

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

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