jQuery自动完成仅适用于大写

时间:2016-03-22 10:26:11

标签: jquery autocomplete

我有一个带有一些自定义PHP编码的WordPress网站。我不知道有什么变化,但我注意到我的自动填充字段不再正常工作。

我有一个表单字段,我输入了播放器名称,输入了三个字符后,我从数据库中检索可能匹配的列表以供选择。我今天已经注意到,它仍在工作,但仅在我输入player2字段时使用所有大写字母的情况下。如果我使用常规字母,我总是会从数据库中获得前10条记录,而不管搜索词是什么。奇怪!!!

这是我的表单jquery代码:

<script type="text/javascript">
    jQuery(document).ready(function($){
        $('#player2').autocomplete({
            source:'/wp-content/themes/canvas-child/suggest-pid2.php', 
            minLength:2,
            autoFocus: true,
            select: function (event, ui) {
                    // Set autocomplete element to display the label
                    this.value = ui.item.playername;

                    // Store value in hidden field
                    $('#PlayerID').val(ui.item.value);

                    // Prevent default behaviour
                    return false;
            }
        });
    });
</script>

这是我的搜索代码,用于从db获取记录:

<?php

// get search term (player name or surname)
$term = $_REQUEST['term'];

if (isset($term)) {    


    $dblink = mysql_connect('localhost', 'un', 'pw') or die( mysql_error() );
    mysql_select_db('db');
    $sql = "SELECT " .
            "P.PlayerID, P.PlayerName, P.PlayerSurname, C.CityName, " .
            "DATE_FORMAT(P.PlayerDateOfBirth, '%Y') AS PlayerYearOfBirth " .
        "FROM players P " .
            "LEFT JOIN cities C ON C.CityID = P.CityID " .
        "WHERE P.PlayerName LIKE '" . $term . "%' " .
            "OR P.PlayerSurname LIKE '" . $term . "%' " . 
        "ORDER BY P.PlayerName, P.PlayerSurname ASC LIMIT 0,10;";


    // Important - UTF-8!!!
    mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $dblink);
    $rs = mysql_query($sql, $dblink);
    $data = array();
    if ( $rs && mysql_num_rows($rs) ) {    
        while( $row = mysql_fetch_array($rs, MYSQL_ASSOC) ) {        
            $data[] = array(            
            'label' => $row['PlayerName'] .' '. $row['PlayerSurname'] .' ('. $row['PlayerYearOfBirth'] .'), '. $row['CityName'] ,            
            'value' => $row['PlayerID'] ,            
            'playername' => $row['PlayerName'] .' '. $row['PlayerSurname']        
            );    
            }}
    echo json_encode($data);
    flush();

}
?>

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

您可以将字符串转换为大写以进行查询

答案 1 :(得分:0)

我从头开始重新创建文件,现在它正在运行。我不知道出了什么问题,但看起来php文件已损坏。现在就工作。