0

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>
2
  • Because that's what you seem to be doing inside the controller. Loose the '%'s and it should work as you want. Commented Nov 23, 2017 at 16:24
  • hey can you mark the answer as correct if it worked for you Commented Sep 9, 2018 at 22:58

2 Answers 2

1

its because you are using LIKE statement for finding phone numbers

Client::where('phone','LIKE','%'.$query.'%')->get();

if you need to show the names of the users who have the exact same match of the number change the above line to

Client::where('phone','=',$query)->get();

or

Client::where('phone','LIKE',$query)->get();

EDIT:

Additionally if you want the name to be cleared from the name input if there are no results returned in the response you should add the following inside the $.get change it to look like following.

return $.get(path, {
    query: query
}, function (data) {

    (data.length == 0) ? $("#name").val(''): '';
    for (var i = 0; i < data.length; i++) {
        obj = data[i];
        $("#name").val(obj.name);
    }
    return process(data);
})

This line (data.length==0)?$("#name").val(''):''; will track if there are no results returned from the response it will clear out the value in the name input box

Sign up to request clarification or add additional context in comments.

4 Comments

now when i clear the phone input box, the name input still remains. when i begin clearing the phone input box, the name should clear out
what do you mean by name input still remains which name are you talking about and which of the above solution are you using LIKE or =
i am using Client::where('phone','LIKE',$query)->get(); When i type in a number, the name shows now. But when i start clearing the phone number, the name in the name input must also clear out
ahh you mean the other input name, let me update the answer
0

Try not using the first '%', that way it'll match the beginning portion of the number without trying to match within

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.