jQuery自动完成功能返回表中的所有行,而PHP根据查询返回结果

时间:2018-12-11 07:17:53

标签: javascript php jquery web-applications jquery-ui-autocomplete

奇怪的问题-Javascript比php返回的访问了更多数据(或者看起来就是这样)。

Javascript:无论我通过查询搜索什么内容( term ),以下代码都将从表中返回所有记录。

$(document).ready(function() {
        //autocomplete
        $(function() {

            $("#search").autocomplete({
                    source: function(request, response) {
                        $.ajax({
                            url: "fetch_emojis.php",
                            method: 'post',
                            data: { term: request.term },
                            dataType: 'json',
                            success: function(data) {
                                response(data);
                            }
                        });
                    },
                    minLength: 1,
                    open: function(event, ui) {
                        $(".ui-autocomplete").css("z-index", 1000);
                    },
                })
                .autocomplete("instance")._renderItem = function(ul, item) {
                    return $("<li><div><img src='" + item.img + "'><span>" + item.description + "</span></div></li>").appendTo(ul);
                };

        });
    });

PHP:以下代码,如果单独运行,则会根据查询返回行(包括示例结果集)

<?php

if ( !isset($_REQUEST['term']) ) {
    exit;
}

$DB_host = "hostname"; 
$DB_login = "userid";
$DB_pass = "password";
$DB_select = "dbname";
$DB_port = "portnumber";

$mysqli = mysqli_connect($DB_host,$DB_login,$DB_pass,$DB_select,$DB_port);

$term = trim(strip_tags($_GET['term'])); 
$term = preg_replace('/\s+/', ' ', $term);

$a_json = array();
$a_json_row = array();

$a_json_invalid = array(array("id" => "#", "value" => $term, "label" => "Only letters and digits are permitted..."));
$json_invalid = json_encode($a_json_invalid);

if(preg_match("/[^\040\pL\pN_-]/u", $term)) {
  print $json_invalid;
  exit;
}


if ($data = mysqli_query($mysqli, "SELECT * FROM emojis WHERE description LIKE '%$term%'")) {
    while($row = mysqli_fetch_array($data)) {
        $id= htmlentities(stripslashes($row['id']));
        $description = htmlentities(stripslashes($row['description']));
        $source = htmlentities(stripslashes($row['source']));
        $a_json_row["id"] = $id;
        $a_json_row["description"] = $description;
        $a_json_row["img"] = $source;
        array_push($a_json, $a_json_row);
    }
}

echo json_encode($a_json);

?>

term =“咧嘴”的PHP结果:

  

[{“ id”:“ 1”,“ description”:“咧着嘴笑   脸部”,“ img”:“ https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/72/apple/155/grinning-face_1f600.png”},{“ id” :“ 5”,“描述”:“咧着嘴笑   面对大   眼睛”,“ img”:“ https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/72/apple/155/smiling-face-with-open-mouth_1f603.png” },{“ id”:“ 6”,“ description”:“咧着嘴笑   面带微笑   眼睛”,“ img”:“ https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/72/apple/155/smiling-face-with-open-mouth-and- Smiling-eyes_1f604.png“},{” id“:” 7“,” description“:”咧着嘴笑   面对   汗水”,“ img”:“ https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/72/apple/155/smiling-face-with-open-mouth-and- cold-sweat_1f605.png“},{” id“:” 8“,” description“:”咧着嘴笑   quin眼   脸部”,“ img”:“ https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/72/apple/155/smiling-face-with-open-mouth-and- ightly-closed-eyes_1f606.png“},{” id“:” 102“,” description“:”咧着嘴笑   猫   脸部”,“ img”:“ https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/72/apple/155/smiling-cat-face-with-open-mouth_1f63a。 png“},{” id“:” 103“,” description“:”咧着嘴笑   猫脸带着笑容   眼睛”,“ img”:“ https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/72/apple/155/grinning-cat-face-with-smiling-eyes_1f638。 png“}]

是否有关于我可能做错事情的想法?

0 个答案:

没有答案