I wanted to assign a returned value (just text) to a variable in jQuery. I wrote this:
var hm=22;
$.ajax({
type: "GET",
url: "ajax_check_match.php",
dataType: "text",
success:callback
});
function callback(data, status)
{
// assign ajaxed value to cm variable
cm=data;
if (hm != cm)
{
dosomething();
}
}
But it fails every time. Why is that the cm variable keeps getting undefined when it sends request. I set the php file to return 1 and it still says undefined. I opened ajax_check_match.php in browser and I see "1".
I didn't see the point of using XML or JSON since a simple number would suffice. Or do I have to use XML/JSON?
UPDATE: as for finding the undefined result... I wrote $("msgbox").append("hm is "+hm+ " and cm is + cm) after the callback function and cm always return undefined. hm is printed 22. It set intervals between three seconds so it keeps appending hm is 22 and cm is undefined.
NEW UPDATE: I am an idiot. Period. The ajax_check_msg.php was in the wrong location - different code. I checked the wrong page. But rest assured, I sure appreciate your suggestions!