doudeng1870 2011-06-07 07:13
浏览 49
已采纳

如何通过php mysql查询获取javascript中的数据

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

  • 写回答

6条回答 默认 最新

  • dongsha2792 2011-06-07 07:28
    关注

    Another solution - instead of creating global variable, set that data as an data- attribute of some related element. Don't forget to perform json_encode() on whole array/object and then htmlspecialchars() on resulting string (as others have noted, lack of htmlspecialchars() is the reason why you got only "{[" when alerting value of input field). Don't forget to specify ENT_QUOTES, when calling htmlspecialchars(), or you might have invalid results if any value contains apostrophe character.

    <?php
        $users = array(
            array('name' => 'Average', 'surname' => 'Joe'),
            array('name' => 'Average', 'surname' => 'Jane'),
            array('name' => 'Joe', 'surname' => 'Sixpack'),
            array('name' => 'John', 'surname' => 'Doe'),
            array('name' => 'Hong', 'surname' => 'Gildong'),
            array('name' => 'Foo', 'surname' => 'Bar'),
        );
    ?>
    <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
    <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
    <head>
        <title>sandbox</title>
        <meta http-equiv='content-type' content='text/html; charset=UTF-8'/>
        <script type='text/javascript' src='js/jquery-1.6.1.min.js'></script>
    </head>
    <body data-users='<?php echo htmlspecialchars(json_encode($users), ENT_QUOTES); ?>'>
        <script type='text/javascript'>
            $(function() {
                var users = $(document.body).data('users');
                for ( var n in users ) {
                    $('<div/>').text(users[n].name + ' ' + users[n].surname).appendTo(document.body);
                }
            });
        </script>
    </body>
    </html>
    

    This makes 6 <div/> elements with all names and surnames as their contents.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题