Best resource on DOM events: http://www.quirksmode.org/dom/events/keys.html
It looks like your event handler "search_buddy" fires an AJAX request, which is asynchronous. The rest of the function runs in parallel with the AJAX request, so "num" is undefined before the $.post returns.
// num is undefined here... unless your network has 0 latency
$("#Layer7").html(num);
You need to wrap this code (pasted above) in a callback function. It appears to be parameter number 3:
http://api.jquery.com/jQuery.post/
Untested, but best guess:
function search_buddy() {
$.post("num.php", function (ret) {
num=ret;
$("#Layer7").html(num);
});
}
Here are some modifications to help you understand:
function search_buddy() {
alert("before the $.post execution");
$.post("num.php", function (ret) {
num=ret;
$("#Layer7").html(num);
alert("$.post callback");
});
alert("immediately after the $.post execution");
}
Note: "alert" will halt all JavaScript processing so you will be able to see what events occur when.