i want to display 700 products data in a select box and on per selection i have to print a table of description . before i was doing ajax call and showing the data but the process is slow. So i have to send all the data to the client side and then use javascript to change the table of description on option change. Below is my code
<?php
$data = json_encode( get_data_from_db() );
?>
<input type='hidden' name='data' id='data' value= " <?php echo $data; ?>" />
when i do alert in jquery
alert( $("#data").val() );
i only see
{[
value in the alert but when i print the data on the browser it does print in json format ok.
how can i get this data in the javascript and then i have to use loops to extract the data against a specific id and make ul li out of that and then embedd it. please suggest the right approach and whats wrong with the alert. thanks
Another solution – instead of creating global variable, set that data as an
data-attribute of some related element. Don’t forget to performjson_encode()on whole array/object and thenhtmlspecialchars()on resulting string (as others have noted, lack ofhtmlspecialchars()is the reason why you got only “{[” when alerting value of input field). Don’t forget to specifyENT_QUOTES, when callinghtmlspecialchars(), or you might have invalid results if any value contains apostrophe character.This makes 6
<div/>elements with all names and surnames as their contents.