I have a form field, called customer. I have a 2nd form field called "gatecode". I want to be able to select a customer(which works right now via an autocomplete script), and upon select of an existing customer, auto-populate the corresponding "gatecode" for that customer. Is this possible?
Right now what happens is the 2nd form field is blank upon load, and it overwrites the "gatecode" whenever the form is saved.
Corresponding Customer Code :
<input data-type="customerName" value="<?php echo isset($invoice['Client']['customerName']) ? $invoice['Client']['customerName']: ''; ?>" type="text" class="form-control" id="clientCompanyName" placeholder="Company Name">
Gate Code
<input type="text" class="form-control" id="customer_GateCode_modal" name="customer_GateCode_modal" placeholder="Gate Code">
Current Jquery code :
$('#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: 2,
select: function( event, ui ) {
if( typeof ui.item.data !== "undefined" ){
var names = ui.item.data.split("|");
$(this).val(names[1]);
getClientAddress(names[0]);
//getAlarmCode(names[0]);
//getOtherData(names[0]);
//getOtherData2(names[0]);
//getOtherData3(names[0]);
//getOtherData4(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);
}
});
}
function getAlarmCode(id){
$.ajax({
url: "ajax.php",
method: 'post',
data:{id:id, type:'alarmcode'},
success: function(result){
$("#alarmcode").html(result);
}
});
}
Current Post Code(for alarmcode value) :
if(isset($_POST['type']) && $_POST['type'] == 'alarmcode' ){
$id = $_POST['id'];
$query = "SELECT id, alarmcode FROM customers WHERE id =$id";
$result = mysqli_query($db->con, $query);
$data = mysqli_fetch_assoc($result);
$alarmcode = '';
if(!empty( $data )){
$alarmcode = isset($data['alarmcode'])? $data['alarmcode']: '';
}
mysqli_close($db->con);
echo $alarmcode;exit;
}