dongrong5189 2012-06-23 02:26
浏览 7
已采纳

从PHP获取数据到jQuery用于Hovercard - JSON

ok so i am trying to use HoverCard on my website for the users that are registered on my site. how to i manage to get the data off the database using PHP and sending it to my jQuery script?

i know how to get data from PHP using jQuery but for some reason it is not working. Here is my code:

jQuery code:

var user = null;
$.post('userjson.php',{},
    function(output) {
        user = output;
    });

$('#loggedUser').hovercard({
    showCustomCard: true, 
    customCardJSON: user
});

PHP code:

$username = $_COOKIE['username'];

$user_select = mysql_query("SELECT * FROM users WHERE username='$username'",$con);
$user_array = mysql_fetch_array($user_select);

$user = array(
    'name' => $user_array['username'],
    'bio' => $user_array['bio'],
    'image' => $user_array['propic']
);

echo json_encode($user);
  • 写回答

1条回答 默认 最新

  • dongnianchou7047 2012-06-23 02:28
    关注

    The POST is asynchronous so you must create the hover card in the callback

    var user = null;
    $.post('userjson.php',{}, function(output) {
        user = output;
        $('#loggedUser').hovercard({
            showCustomCard: true, 
            customCardJSON: JSON.parse(user)
        });
    });     
    

    In your code the user variable was not set when you tried to create the hover card.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计