Pranjali
Pranjali

Reputation: 535

Issue with jQuery appendTo Function

I have designed a form where the user can enter his comment using "Add Comment" button. whatever comment user enters is added to the list of Comments on the page. I am appending new comment using "appendTo()" function. but whenever I refresh the page all the comments added using "appendTo()" function are lost. My question is, is there any way to retain the comments even after "Page Refresh"? Any hint or a demo example would be very helpful. Thanks in advance..

Upvotes: 2

Views: 153

Answers (3)

RonnyKnoxville
RonnyKnoxville

Reputation: 6394

It seems like you have a couple of options here. You could create a database for the back end or you could get the script to save the comments to an xml document. That the comments could be easily viewed by an RSS reader also.

Upvotes: 0

gdoron
gdoron

Reputation: 150263

Is there any way to retain the comments even after "Page Refresh"?

No you can't. If you "save" the data only in the client side it will not be saved after Page refresh. HTTP is stateless. once the response reached the client, the server doesn't "know" the client anymore.

If you want the comment to be saved, You will have to use some sort of Data Base that stores the comments.

Additional to the appending you should make an AJAX post request to add the comment to the server.

Upvotes: 2

Pierre de LESPINAY
Pierre de LESPINAY

Reputation: 46188

You need to store the comments in the server, ie a database.

I suggest using a form filled with each new comments and ajax posted when a user sumits.

<form action="post_comments" method="POST">
  <input id="new_comment" type="hidden" name="comment" />
</form>

<div id="comments">
  <input type="button" id="add_comment" value="Add Comment" />
  <textarea>comment 1</textarea>
  <textarea>comment 2</textarea>
</div>

So your comments will be kept at refresh.
Do you know how to post it with ajax ?

Upvotes: 0

Related Questions