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 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?