Carlos Veloz Carlos Veloz - 1 month ago 4x
Ajax Question

Ajax result seems to concatenate to previous results

I have an ajax function that upload a comment to a form which returns the same string and if it success, the comment is pretended to a comment box and the comment input text is cleaned.

The problem is that if the user add another comment the function with add this new comment + the past comment to the comment box, where the first comment is already displayed.

i have a walk around where before the pretend i empty the comment box but this is not the best behavior.

this is my ajax function:



var formdata = new FormData(this);

url: "php/comment_form.php",
type: "POST",
data: formdata,
contentType: false,
cache: false,
processData: false,
success: function(result){
},error: function(){

So for example i add a comment : "Hello"

The div#new_comments will display:


(Thats the result from the php form)

But if i add a new comment: "World"
Then the div#new_comment will show:


And so..

Why is this happening?
I tried add a var with the result like

var comment = result;

Then after the comment is prepend:

var comment = "";

But it doesn´t work.

Any ideas?


Try .html("")