Arif Arif - 6 months ago 29
MySQL Question

jQuery Autocomplete doesn't load more than 250 MySQL records?

I have a form of product where product name fild has an autocomplete feature but it doesn't load more than 250 records. If I put the limit to 300 or 500 then it doesn't work. Below is my code, can anybody tell me if there is any issue in my script.

jQuery.noConflict();
jQuery(function() {
var availableTags = [
<?php
$query_product = "SELECT DISTINCT prod_name FROM tbl_product LIMIT 250";
$result_product = mysql_query($query_product);
while($row_product = mysql_fetch_array($result_product))
{
$prod_name = $row_product['prod_name'];
?>
"<?php echo $prod_name; ?>",
<?php
}
?>
];
jQuery('input[name="item[]"]').autocomplete({
source: availableTags
});
});



<input type="text" placeholder="Item" id="item" name="item[]">

Answer

It is very likely that one of your $row_product['prod_name'] has a single-quote or backslash in it so your database data needs to be sanitized before giving it to Javascript.

I think your code needs to look like this:

var availableTags = [
    <?php
    $query_product = "SELECT DISTINCT prod_name FROM tbl_product LIMIT 250";
    $result_product = mysql_query($query_product);
    while($row_product = mysql_fetch_array($result_product))
    {
        echo "'".str_replace("'", "\\'", str_replace('\\', '\\\\', $row_product['prod_name']))."',";
    }
    ?>
];