Made appendMessage method more flexible

I did this by moving out from the actual Ajax POST request. And made a
method out of it.