Note that there are some explanatory texts on larger screens.

plurals
  1. POComments are shown twice when posted through AJAX, but appear only once on page reloads
    text
    copied!<p>I used the following codes to add comment to a post and display it without refreshing the page, but somehow, it displays the comment twice instead of once, but it only saves it once in the database. When I refresh the page, it displays each comment only once though, so i guess the problem is with the "ajax response". Here are the codes, thanks for any help.</p> <p>JavaScript:</p> <pre><code>function addComment(pid) { var url; var comment = document.getElementById("comment").value; xml_http = getXmlHttpObject(); if (xml_http == null) return alert("Your browser is too old to run this page!"); url = '../auth.php?comment=' + comment + '&amp;pid=' + pid; url += '&amp;p=' + Math.random(); xml_http.onreadystatechange = commentStatus; xml_http.open("GET", url, true); xml_http.send(null); } function commentStatus() { $("#comm").append(xml_http.responseText); } </code></pre> <p>PHP:</p> <pre><code>include_once('include/comment.php'); addComment($_GET['pid'], filter($_GET['comment'])); if(empty($_SESSION['pic'])) $pic = "images/silh.gif"; else $pic = "/profile/image.php/{$_SESSION['uname']}.{$_SESSION['pic']}?width=45&amp;amp;cropratio=1:1&amp;amp;image=/profile/pix/{$_SESSION['uname']}.{$_SESSION['pic']}"; echo "&lt;div id='comments'&gt;\n" ."&lt;img src='{$pic}' align='left' style='padding-right:5px' /&gt;" ."&lt;span id='bluetxt'&gt;By {$_SESSION['uname']}&lt;/span&gt;&lt;br /&gt;\n" ."&lt;span&gt;Posted Now&lt;/span&gt;\n" ."&lt;br /&gt;&lt;br /&gt;&lt;p style='clear:both'&gt;{$_GET['comment']}&lt;/p&gt;" . "&lt;/div&gt;&lt;br /&gt;\n"; </code></pre> <p>I purposely don't read the comment from the database, I simply post it back on the page.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload