我正在努力解决以下问题。我想用PHP创建一个实时搜索,从dB中检索值,并将多个选定的值存储回SQL表中。
我今天访问的许多网站都是这样的,但没有找到完美的解决方案。
<input name="find" id="find" value=""/>
<ul class="selectedItems"></ul>
$("#luogoNascita").autocomplete({
minLength: 3,
dataType: "json",
source: function(req, add){
$.ajax({
url: url,
dataType: 'json',
type: 'POST',
data: { find : $("#find").val() } ,
success: function(data){
if(data.result){
add(data.result);
}
else{
$(this).val('');
}
}
});
},
select: function( event, ui ) {
if(ui.item)
{
var obj = $.parseJSON(ui.item.value);
}
else{
$(this).val('');
}
},
close: function(event, ui) {
var obj = jQuery.parseJSON($( this ).val()) || {};
var id = obj.id || null;
if (id){
$( this ).val(obj.selected);
$.post(saveUrl,{id: id},function(res){
console.log(res);
}) ;
}
$('.selectedItems')
.append('<li>'+obj.selected+'</li>');
$(this).val(''); // clear input
}
});
//服务器端PHP//url.PHP
$data['result'] = array();
$data['result'][] = array(
'label'=> 'First',
'value'=> json_encode(array(
'id' => 1,
'selected' => 'First'
))
);
$data['result'][] = array(
'label'=> 'Second',
'value'=> json_encode(array(
'id' => 2,
'selected' => 'Second'
))
);
echo json_encode($data);
//服务器端PHP//saveUrl.PHP
$_POST['id'];
//save in DB function;