views:

37

answers:

1

hello guys, i got this code that keep on returning undefined msg instead of the expected html. Purpose of function: the purpose of the below function is to return notifications in away similar to fb one's. the code is working fine. but there's something wrong with the getJSON part that i couldn't figure out. so instead of returning "clonex1 likes your post" i get undefined. the code

function buzzme()
{
jQuery('#cnumber').removeClass();
jQuery('#cnumber').empty();

jQuery('#floating_box').toggle();

 var nHeader = '<div id="floating_box" class="fb">' +
'<div id="header"><span id="htext">Notifications</span></div>' +
'<div id="int">' +
'<div id="bodyx">' +
'<ul>';
var nFooter = '</ul>' +
'<div class="jfooter">' +
'<a href="#" id="seemore">See all notifications</a>' +
'</div>' +
'</div>' +
'</div>' +
'</div>';
var nContent;

jQuery.getJSON('notifications.php', {'n':1,'dht':3692}, function(response){
    jQuery.each(response, function(i, nt2){
        nContent += '<a href="#"><li id="lix">sdfsdfsd'+nt2.img+' '+nt2.notifier+'</li></a>';

    })

});

alert(nContent);
var nFinal = nHeader+nContent+nFooter;
if (!jQuery('#floating_box').length) {
  jQuery('body').append(nFinal);
}
}

notifications.php - setUpFlayout(); and setUpJSONList()

    function setUpFlyout() {
    $notify = new se_notify();
    $data2 = $notify->notify_summary();
    $trk = 0;

    if($data2['total'] >= 1) {
    for($i = 0; $ $i <= $data2['total']; $i++) {
$nid = $data2['notifys'][$i]['notify_id'];
$im = $data2['notifys'][$i]['notify_icon'];
$img = "<img src='./images/icons/$im' />";
$notifier = $data2['notifys'][$i]['notify_text'][0];
$atype = $data2['notifys'][$i]['notifytype_id'];
$url = '';
$url2 = $data2['notifys'][$i]['notify_url'];
if($atype == 1) {
  $url = ' has sent you friend <a href='.$url2.'>request</a>';  
}
$trk++;    
if($data2['total'] >= 2) {
    $ret_arr = '';
     if($i == 0) {
         $ret_arr = '[';
     }
     $ret_arr = $ret_arr.setUpJSONList($data2['total'], $nid, $img, $notifier, $url, $trk);
     if($i == $data2['total']-1) {
         $ret_arr = $ret_arr.']';
     }
    echo ''; 
} else if($data2['total'] == 1){
   //$ret_arr = '[{"dh3":"'.$data2['total'].'","nid":"'.$nid.'", "img":"'.$img.'","notifier":"'.$notifier.'","url":"'.$url.'"}]';
   $ret_arr = '';
   echo $ret_arr;
}
    if($i == ($data2['total']-1))
        break;
        }
    }
}

setUpJSONList();

function setUpJSONList($total, $nid, $img, $notifier, $url, $track) {
    $comma = ',';
    $lp = '';
    $rp = ']';
    $result = '';
    if($track == $total) {
    $result = '{"pos":"'.$track.'","dh3":"'.$total.'","nid":"'.$nid.'","img":"'.$img.'","notifier":"'.$notifier.'", "url":"'.$url.'"}';
    } else {
        $result = '{"pos":"'.$track.'","dh3":"'.$total.'","nid":"'.$nid.'","img":"'.$img.'","notifier":"'.$notifier.'", "url":"'.$url.'"},';    
    }
    return $result;
}

thanks

+3  A: 

Your usage of nContent after getJSON might be undefined since getJSON is asynchronous and would not have completed initializing nContent. You need to move the code using nContent inside the callback of getJSON.

jQuery.getJSON('notifications.php', {'n':1,'dht':3692}, function(response){
    jQuery.each(response, function(i, nt2){
        nContent += '<a href="#"><li id="lix">sdfsdfsd'+nt2.img+' '+nt2.notifier+'</li></a>';

    })
    alert(nContent);
var nFinal = nHeader+nContent+nFooter;
if (!jQuery('#floating_box').length) {
  jQuery('body').append(nFinal);
}
});
Marimuthu Madasamy
+1 - This is correct, it's not a "might be" either, it's guaranteed to be undefined at the point it's being used (single-threading and such).
Nick Craver
am sorry for this am kinda fuzzy. can you demo that in a snippet?
clonex1
@clonex1 - Updated with code
Marimuthu Madasamy
Thanks Marimuthu, that worked but its still showing undefined. here's screenshothttp://hinuts.com/yt5.PNG
clonex1
Screenshot is not opening for me. But I guess since nContent is initially undefined and you are appending strings to it, initial undefined is converted to string and precedes your string. Have var nContent = ''; //initialize with blank string
Marimuthu Madasamy