$.post() asynchronous return value problem

$.post() asynchronous return value problem

Hi,

I am having problems with my $.post function returning a undefined value. I read somewhere this is because the function returns the value before it gets a result from the server.

I am not quite sure how to get around this...

  1. function addMediaCategory(name,parent_id)
  2. {
  3. $.post("addMediaCategory.php",{cat_name:name,parent:parent_id},
  4. function(data)
  5. {
  6. if(data == "ok")
  7. {
  8.  return "ok";
  9. }
  10. else
  11. {
  12.  return "error";
  13. }
  14.     },"text");
  15. }

  16. function callMediaAdd()
  17. {
  18. var returnVal = addMediaCategory("Cat1",1);
  19. alert(returnVal);
  20. }
The returnVal is undefined...

Any help?

Thanks,
Cheers