我正在尝试更改一些给我的自动完成代码。选择一个人后,现有代码会自动填写人员地址。我想改变它以自动填写地址并插入自定义字段。以下是我对代码进行的更改以尝试此操作。我正在寻找一个如何使用此代码“自动填充”多个项目或指向教程的链接的工作示例。
自动填充代码:
$('#clientCompanyName').autocomplete({
source: function( request, response ) {
$.ajax({
url : 'ajax.php',
dataType: "json",
method: 'post',
data: {
name_startsWith: request.term,
type: 'customerName'
},
success: function( data ) {
if(!data.length){
var result = [
{
label: 'No Client found',
value: ''
}
];
response(result);
}else{
response( $.map( data, function( item ) {
var code = item.split("|");
return {
label: code[1],
value: code[1],
data : item
}
}));
}
}
});
},
autoFocus: true,
minLength: 1,
select: function( event, ui ) {
if( typeof ui.item.data !== "undefined" ){
var names = ui.item.data.split("|");
$(this).val(names[1]);
getClientAddress(names[0]);
// Added getClientField2(names[0]);
$('#client_id').val( names[0] );
}else{
return false;
}
}
});
function getClientAddress(id){
$.ajax({
url: "ajax.php",
method: 'post',
data:{id:id, type:'clientAddress'},
success: function(result){
$("#clientAddress").html(result);
}
});
}
//added function getClientField2(id){
$.ajax({
url: "ajax.php",
method: 'post',
data:{id:id, type:'clientField2'},
success: function(result){
$("#clientField2").html(result);
}
});
}
服务器代码:
if(isset($_POST['type']) && $_POST['type'] == 'clientAddress' ){
$id = $_POST['id'];
$query = "SELECT id, addressLine1 FROM customers WHERE id =$id";
$result = mysqli_query($db->con, $query);
$data = mysqli_fetch_assoc($result);
$address = '';
if(!empty( $data )){
$address = isset($data['addressLine1'])? $data['addressLine1'].' ' : '';
}
mysqli_close($db->con);
echo $address;exit;
}
if(isset($_POST['type']) && $_POST['type'] == 'clientField2' ){
$id = $_POST['id'];
$query = "SELECT id, field2 FROM customers WHERE id =$id";
$result = mysqli_query($db->con, $query);
$data = mysqli_fetch_assoc($result);
$field2 = '';
if(!empty( $data )){
$field2 = isset($data['field2'])? $data['field2'].' ' : '';
}
mysqli_close($db->con);
echo $field2;exit;
}