I have two inputs name and phone inputs. When I type a phone number, the name of the of the client should appear in the name input box.
With my code below, what happens is, when i start typing a new number which doesn't exist, the query selects the name of a person whose number is similar. Why is this happening?
What I want is, if the number doesn't exist, the query should not populate my name textbox.
HTML
<div class="form-group">
<label for="name" class="col-lg-1 control-label">Phone</label>
<div class="col-lg-8">
<input type="text" class="typeahead form-control" id="phone" placeholder=" Customer Phone Number" autocomplete="on" name="phone" required>
</div>
</div>
<div class="form-group">
<label for="name" class="col-lg-1 control-label"> Name</label>
<div class="col-lg-8">
<input type="text" class="form-control" id="name" placeholder=" Customer Name" name="name" required>
</div>
</div>
Controller
public function autocomplete(AutoCompleteFormRequest $request)
{
$query = $request->get('query','');
$client = Client::where('phone','LIKE','%'.$query.'%')->get();
return response()->json($customer);
}
JS
<script type="text/javascript" >
var path = "{{ route('autocomplete') }}";
var obj;
$('input.typeahead').typeahead({
source: function (query,process){
return $.get(path, {query:query},function (data)
{
for(var i = 0; i < data.length; i++) {
obj = data[i];
$("#name").val(obj.name);
}
return process(data);
})
}
});
</script>