danish hashmi
danish hashmi

Reputation: 482

Autocomplete only working for the first time

<script type="text/javascript" src="js/jquery.js"></script>
<script type='text/javascript' src='js/jquery.autocomplete.js'></script>
<link rel="stylesheet" type="text/css" href="css/jquery.autocomplete.css" />

The autocomplete works for the first time when the page is refreshed and i enter upto 4 character if i backspace and enter other character from start it doesn't work , can someone help me why is this happening

$().ready(function() {


    $("#customername").autocomplete("ajax/customerlist.php", {
        width: 260,
        dataType: 'json',
        matchContains: false,
        selectFirst: false,
        minlength:4,
        parse: function(data) {

                var array = new Array();
                for(var i=0;i<data.items.length;i++)
                {       //alert(data.items[i].name);
                        array[array.length] = { data: data.items[i], value: data.items[i], result: data.items[i].name };
                }
                return array;
        },
        formatItem: function(row) {                     
                var name = '';
                if (row.first_name && row.last_name)
                        name = '('+row.first_name+', '+row.last_name+')';
                else if (row.first_name)
                        name = '('+row.first_name+')';
                else if (row.last_name)
                        name = '('+row.last_name+')';

                return row.name+' '+name;
        }
    });

    $('#customername').result(function (event, data, formatted) {
      $( "#cust" ).val( data.id );
      var add= data.add1+data.add2;
      $( "#address" ).val( add );
      $( "#email" ).val( data.email );
      $( "#phone" ).val( data.phone );
      $( "#mobile" ).val( data.mobile );

    });
});

the php code is

$getuser=$db->execute("select * from `customer` where customer_name like '%".mysql_$_REQUEST['q']."%'  ");


while($getuserlist = $db->fetchAll($getuser)) {

$list['items'][]=array('name' => $getuserlist['customer_name'],'id'=>$getuserlist['customer_id'],'add1'=>$getuserlist['add1'],'add2'=>$getuserlist['add2'],'city'=>$getuserlist['city'],'state'=>$getuserlist['state'],'pincode'=>$getuserlist['pincode'],'email'=>$getuserlist['email'],'phone'=>$getuserlist['phone'],'mobile'=>$getuserlist['mobile']);

}

echo json_encode($list);

Upvotes: 0

Views: 3254

Answers (1)

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

change this

$().ready(function() {

to

$("#customername").keyup(function() {

it will run whenever you type in "#customername"

Upvotes: 2

Related Questions