Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

autocomplete label not displaying data

I am trying to autocomplete data from MySQL database with the bellow given script. It is not displaying label while it is giving correct response in developer tool network.

JS

$('#search').autocomplete({
            source: function( request, response ) {
                $.ajax({
                    url: 'search.php',
                    dataType: "json",
                    method: 'post',
                    data: {
                        name_startsWith: request.term,
                        type: 'type'
                    },
                    success: function( data ) {
                        response( $.map( data, function( item ) {
                                return {
                                    label: code['id'],
                                    value: code['id'],
                                    data : item
                                }
                            }));
                        }
                    });
            },
            autoFocus: true,            
            minLength: 1,
            select: function( event, ui ) {
                $('#player').val(ui.item.data.player);
                $('#marks').val(ui.item.data.marks);
            }               
        });  

search.php

$type = $_POST['type'];
$id = $_POST['name_startsWith'];
$query = $db->prepare("SELECT id, player, marks, note FROM players where ( id LIKE '".$id."%') ");
$query->execute();
$data = array();

$i = 0;
while ($row = $query->fetch(PDO:: FETCH_ASSOC)) {

    $data[$i]['id'] = $row['id'];
    $data[$i]['player'] = $row['player'];
    $data[$i]['marks'] = $row['marks'];
++$i;
}  
echo json_encode($data);

Network response

[{"id":"4133","player":"Sam","marks":"65"},{"id":"4955","player":"valiu","marks":"34"}]
like image 298
Dishko Avatar asked Mar 10 '26 00:03

Dishko


1 Answers

Replace this:

 return {
     label: code['id'],
      value: code['id'],
      data : item
 }

With:

return {
     label: item['id'],
      value: item['id'],
      data : item
 }

You've wrong parameter passed.

Also from fiddle, update these and then test

change url as url: 'https://jqueryui.com/resources/demos/autocomplete/search.php?term=ro' and change dataType as dataType: "jsonp"

then test

like image 127
Rajender Verma Avatar answered Mar 12 '26 13:03

Rajender Verma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!