Shahsays
Shahsays

Reputation: 431

jQuery Autocomplete Not showing all results from query

i am using jQuery auto complete to view results as drop down as user types, in my case i am getting user names from database to show,

 <?php 
          foreach ($stmt = $link->query('select u.id, u.display_name  from user u inner join accounts a on u.id=a.uid  where a.account_type !="bot" order by u.display_name ASC') as $value) 
          {
            ?>
              display_name.push('<?php echo $value["display_name"] ?>');
              user_id.push('<?php echo $value["id"] ?>');
        <?php 
          }
        ?>

the problem here is, alot of names are like o'livia or de'silva, this extra ' is causing the query to behave unexpectedly, it does not show results and instead prints this error message.

 user_id.push('5648');
 display_name.push('Boody L'Dally');
 Uncaught SyntaxError: missing ) after argument list

while i know i am not formatting my query right, i have tried a lot of options but of no use, any idea how to bring all the names correctly?

Upvotes: 0

Views: 59

Answers (3)

Punnoose Varghese
Punnoose Varghese

Reputation: 1

function autocomplet() {
    var min_length = 0;
    var keyword = $('#country_id').val();
    if (keyword.length >= min_length) {
        $.ajax({
            url: 'ajax_refresh.php',
            type: 'POST',
            data: {keyword:keyword},
            success:function(data){
                $('#country_list_id').show();
                $('#country_list_id').html(data);
            }
        });
    } else {
        $('#country_list_id').hide();
    }
}


function set_item(item) {
    // change input value
    $('#country_id').val(item);
    // hide proposition list
    $('#country_list_id').hide();
}

Upvotes: -1

Ye Lwin Soe
Ye Lwin Soe

Reputation: 356

if your string just include only ' never " ... so can solve like below .. user_id.push('5648'); display_name.push("Boody L'Dally");

Upvotes: 2

Dylan James McGannon
Dylan James McGannon

Reputation: 944

You need to escape the data coming from the database. There are many functions to escape data and it's worth reading into further. Here's an example for you:

display_name.push('<?= htmlspecialchars($value["display_name"]) ?>');

Upvotes: 1

Related Questions