开发者

AJAX response from database creates duplicate entries

开发者 https://www.devze.com 2023-03-02 19:51 出处:网络
I am trying to build a simple forum where the questions wil b开发者_开发百科e refreshed automatically . Although it is done ,I have two problems.

I am trying to build a simple forum where the questions wil b开发者_开发百科e refreshed automatically . Although it is done ,I have two problems. 1 . Since I retrieve the last update from database,last entry is dulicated. 2 . The div where I put the AJAX response always refrshes . How can I remove the duplicate entries and make it smooth forum ,just like in facebook comment page ?


If I understand your first problem, you're saying you are doing something like:

  1. Take the user's input (i.e. the new entry)
  2. Write this input to your database
  3. Select all entries, including the one you just added

Because you are also separately adding the new reply to the div client-side or something the last entry always shows up twice (but doesn't get written to the database twice). If this is the case you could solve the problem by either omitting the last entry from your MySQL query (e.g. SELECT * FROM entries WHERE id != (SELECT MAX(id) FROM entries)) or by simply refraining from processing the entry on the client's side.

Concerning your second problem: Instead of fetching all entries and replacing all content inside the div with the new list of entries, try only appending the new entry with .innerHTML += ... or jQuery's .append() for example. This way you won't experience any 'flickering' or jumping content with the rest of the replies.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号