dongyanghan0556 2015-04-02 06:54
浏览 30
已采纳

为什么我的自动填充功能没有暗示[关闭]

This is my php file. I want autosuggest for my text box which is not working. I have 3-4 sources of autosuggest.

<?php
$con =mysqli_connect("due connectn attributes");
$return_arr = array();
$search = mysqli_real_escape_string($_GET["term"]);   
if (!$search) return;
$sql = "SELECT PName, PNo FROM PlayerDetails WHERE PName LIKE '%$search%'";
$sql = mysqli_query($sql);

while ($row = mysqli_fetch_assoc($sql)){
    $row_array['PNo'] = $row['PNo'];
    $row_array['PName'] = $row['PName'];
    //echo "$PName|$PNo
";

            array_push($return_arr, $row_array);
            /* Toss back results as json encoded array. */
            echo json_encode($return_arr);
}
mysqli_close($con); 
?>

The javascript part:

 <script src="/_js/jquery-2.1.3.min.js"></script>
 <script src="/_js/jquery-ui.min.js"></script>
 <script>
 $(document).ready(function() {
    $("#auto").autocomplete({
            source: "../_php/search.php",
            autoFocus:true
            }
        });
      });   //end READY

 </script>

The HTML part is:

  <p>
    <label for="Username">Username:</label>
    <input type="text"  name="auto" id="auto">
</p>
  • 写回答

2条回答 默认 最新

  • dongzi8191 2015-04-02 07:46
    关注

    You cannot generate multiple JSON encodes; that gets you no valid JSON, and jQuery will ignore it.

    while ($row = mysqli_fetch_assoc($sql)) {
        $row_array['PNo'] = $row['PNo'];
        $row_array['PName'] = $row['PName'];
        array_push($return_arr, $row_array);
        /* Toss back results as json encoded array. */
        echo json_encode($return_arr);
    

    }

    The echo json_encode($return_arr); must happen only once after the mysqli_close:

        array_push($return_arr, $row_array);
    

    } /* Toss back results as json encoded array. */ echo json_encode($return_arr);

    Also, just in case, output the JSON content-type headers:

    }
    /* Toss back results as json encoded array. */
    Header("Content-Type: application/json; charset=UTF-8");
    die(json_encode($return_arr));
    

    Lastly, you report this error in your comment:

    <b>Warning</b>: include() [<a href='function.include'>function.include</a>]: Failed opening '../_php/search.php' for inclusion (include_path='.:/usr/local/php-5.3.29/share/pear')
    

    This error can be caused by one thing only: you must be including from PHP the search.php script. I.e. in some of your PHP scripts there is a

    include '../_php/search.php';
    

    This must not be done - search.php must be an independent PHP script and it should return a valid JSON if you call its URL from a browser like this:

    http://<yoursite>/_php/search.php?term=%
    

    i.e., it should return something like

    [["PnoNumber1":"PNameNumber1"],["PnoNumber2":"PNameNumber2"]]
    

    File organization (there's more than one way to do it)

    The HTML file can be wherever you want, let's say it is in /_html/page.html

    This file includes the necessary JS code which lives in /_js, so /_js/jquery.min.js and so on.

    The PHP is called by the Javascript code inside the HTML as is detailed in another answer. When you include the 'search.php' URL in your Javascript, this generates a new browser call which has a lifecycle of its own in AJAX. This call must be able to "live" by itself, so it must work also if you use it in your browser's address bar. Otherwise it will not work in the script either.

    So to test that the script is working (of course you can also use tools such as Firebug) you can invoke it from the browser:

    http://<yoursite>/_php/search.php?term=%
    

    The script must then connect to the database and perform the query, returning a valid JSON in the format that Autocomplete expects. So in answer to the call above you must see a valid JSON in the browser window.

    The .autocomplete function is correctly bound with $('#auto') and the input field has indeed an id of 'auto'. So the call will be generated. The URI you put in the javascript must be valid with reference to the javascript, so if the form lives in /_html/searchpage.html, and the script in /_php/search.php, the relative URI will be ../_php/search.php (of course if the form is in /_html/forms/search.html, you will need ../../_php/search.php and so on).

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

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据