MY AIM
Use jQuery UI Autocomplete to search for a book and when a book is clicked from the suggestion dropdown, populate the remainder of the HTML form with details of the book selected by using the select function of the autocomplete.
THE PROBLEM
As long as the returned data does not contain a line break (I have a table field called description in the database and it has line breaks in the stored data ) the autocomplete works like a charm. But if I include the description field data into the returned array, the suggestion dropdown fails to show up. However when checked with Firebug, data is being returned; just not showing up
THE CODE
// STORING INFORMATION FROM THE DATABASE
$items[$r['book_id']] = array('name'=>$r['book_name'],
'category'=>$r['c_id'],
'author'=>$r['book_author'],
'vendor'=>$r['book_publisher'],
'edition'=>$r['book_edition'],
Culprit here --> 'description'=>$r['book_description'],
'price'=>$r['book_price'],
'date'=>$r['date']);
// DATA FOR POPULATING THE HTML FORM (USING ui.item.<index name>)
$result = array();
foreach ($items as $key=>$value)
{
array_push($result, array("id"=>$key,
"label"=>$key."-".$value['name'],
"value" => strip_tags($value['name']),
"name" => $value['name'],
"cat" => $value['category'],
"author" => $value['author'],
"vendor" => $value['vendor'],
"edition" => $value['edition'],
"description" => $value['description'],
"price" => $value['price'],
"date" => $value['date']));
}
// $result is then supplied to a function which converts the data to json
If I remove the description entry from the $items and $result arrays, everything works as expected. I tried a couple of things after going through SO threads here:
WHAT I TRIED
Tried str_replace('\n', '\\n', $r['book_description'])
Tried str_replace('<br />', '\\n', $r['book_description'])
Out of frustration even tried urlencode($r['book_description'])
But none of them seemed to work. Is there a workaround for this one?
Thanks in advance.
UPDATE
Using json_encode on the returned set is resulting into:

Are you using json_encode()? ..It should escape everything for you. You could also try something like
or