Javascript 防止已选择的选项Jquery使用MYSQL自动完成
我使用jqueryautocomplete从mysql表中获取标记列表。当用户从列表中选择一个标记时,它将保存在页面上。我试图阻止已保存的标记再次显示 这是我的密码: HTML PHPJavascript 防止已选择的选项Jquery使用MYSQL自动完成,javascript,php,jquery,html,mysql,Javascript,Php,Jquery,Html,Mysql,我使用jqueryautocomplete从mysql表中获取标记列表。当用户从列表中选择一个标记时,它将保存在页面上。我试图阻止已保存的标记再次显示 这是我的密码: HTML PHP 当我选择第一个建议时,它会保存在#tags中,但不会显示其他建议。如果有任何其他建议可以实现这一点,那就太好了。我想出来了。我试图向准备好的语句传递一个数组。 PDO不是那样工作的。 为了解决这个问题,我首先声明了参数的数量,然后将它们放在prepared语句中,同时使用foreach对每个参数设置bindval
当我选择第一个建议时,它会保存在
#tags
中,但不会显示其他建议。如果有任何其他建议可以实现这一点,那就太好了。我想出来了。我试图向准备好的语句传递一个数组。
PDO不是那样工作的。
为了解决这个问题,我首先声明了参数的数量,然后将它们放在prepared语句中,同时使用foreach对每个参数设置bindvalue
以下是最终解决方案:
//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();
这就解决了问题。我希望它也能帮助其他人。您的查询是什么样子的(log
$query
以查看您真正发送给数据库的内容)?我一直在尝试这样做。我怎么做?任何资源?您可以在PHP页面上创建或打印它(echo)(然后它将出现在您的AJAX调用响应中)。
$('#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('');
}
$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 . "%"));
}
//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();