防止已选择的选项 Jquery 使用 MYSQL 自动完成

Prevent already selected option Jquery autocomplete with MYSQL

本文关键字:MYSQL 使用 Jquery 选择 选项      更新时间:2023-09-26

我正在使用Jquery Autocomplete从mysql表中获取标签列表。当用户从列表中选择一个标签时,它会保存在页面上。我正在尝试防止再次显示已保存的标签。

这是我的代码:

.HTML

<input type="text" id="tag">
<input type="text" id="tags" style="display:none;">

Jquery

$('#tag').autocomplete({
            source : function(request, response) {
                $.ajax({
                    url : 'tags.php',
                    dataType : "json",
                    method : 'post',
                    data : {
                        searchQuery : request.term, 
                        selectedTags: $('#tags').val() //sends already selected terms
                    },
                    success : function(data) {
                        response($.map(data, function(item) {
                            var code = item.split("|");
                            return {
                                label : code[0],
                                value : code[0],
                                data : item
                            }
                        }));
                    },
                    error: function(jqxhr, status, error)
                    {
                        alert(error);
                    }
                });
            },
            autoFocus : true,
            minLength : 1,
            select : function(event, ui) {
                var names = ui.item.data.split("|");
                tag_ids = [];
                tag_names = [];
                tags = $('#tags').val();
                if(tags != '')tag_names = tags.split(',');
                tag_ids.push(names[1]);
                tag_names.push("'" + names[0] + "'");
                $('#tags').show();
                $('#tags').val( tag_names.join( "," ) );
                $('#tag').val('');
            }

.PHP

$searchQuery = $_POST['searchQuery'];
$selectedTags = $_POST['selectedTags'];
if(!empty($selectedTags))
{ $query = $db->prepare("SELECT * FROM tags WHERE name LIKE ? AND name NOT IN ?");
$query->execute(array($searchQuery . "%", $selectedTags));
}
else
{
$query = $db->prepare("SELECT * FROM tags WHERE name LIKE ?");
$query->execute(array($searchQuery . "%"));
}

当我选择第一个建议时,它会保存在#tags但随后不显示其他建议。如果还有其他建议可以实现这一目标,那就太好了。

我想通了。我试图将数组传递给准备好的语句。PDO不是这样工作的。为了解决这个问题,我首先声明了参数的数量,然后将它们放在准备好的语句中,同时使用 foreach 将值绑定到每个参数。

这是最终的解决方案:

//exploding the string to an array first
$selectedTags = explode(',', $selectedTags); 
//creating another array with parameters equal to the size of the selectedTags
$bindValues = implode(',', array_fill(0, count($selectedTags), '?')); 
//putting the parametes
$query = $db->prepare("SELECT * FROM tags WHERE name LIKE ? AND name NOT IN (" . $bindValues .")");

//binding values
$query->bindValue(1, $searchQuery . '%');
//Now, using foreach to bind selected tags values
foreach($selectedTags as $k => $selectedTag)
{
 //using k+2 as index because index starts at 0 and first parameter is the search query
 $query->bindValue($k+2, $selectedTag);
}
$query->execute();

这解决了问题。我希望它也能帮助其他人。