Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaScript Being Called
    text
    copied!<p>So I have a ajax function which appends data to a php page. It works like a comment panel that when you scroll to the bottom you can click a button and it loads more comments. This is the ajax function which loads more comments.</p> <pre><code>$("#loadmorecomments").click(function(){ $('div#loadmorecomments').show(); .ajax({ url: "loadmorecomments.php?lastComment=" + $(".post:last").attr("id") + "&amp;sname=&lt;?php echo $link ?&gt;", success: function(html){ if(html){ $("#postspace").append(html); $("div#loadmorecomments").hide(); else{ ("div#loadmorecomments").replaceWith("div#done"); } }); }); </code></pre> <p>This is the page it appends to the original php page. All of this code including the javacript is part of the loadmorecomments.php which gets appended to the original page. So on the original page where all of this exact same code works. Doesn't work on these new comments that are appended on.</p> <pre><code> if($stmt = mysqli_prepare($mysqli, "SELECT post.text,post.username,post.id,post.likes,post.dislikes,post.image,users.avatar FROM post LEFT JOIN users ON post.username = users.username WHERE post.school='$sname' AND post.id &lt; $lastcomment ORDER BY post.id DESC LIMIT 0,10")){ mysqli_stmt_execute($stmt); mysqli_stmt_execute($stmt); mysqli_stmt_bind_result($stmt,$comments,$user, $id,$likes,$dislikes,$image,$avatar); $counter = 0; while(mysqli_stmt_fetch($stmt)){ /////////////////Get Image/////////////////// switch($user){ case "Anonymous": $img = "design/anonymous.png"; break; case $user != "Anonymous" &amp;&amp; !empty($avatar): $img = $avatar; break; case $user != "Anonymous" &amp;&amp; empty($avatar): $img = "logo.png"; break; } if(!empty($image) || $image != "images/"){ $comimage = $image; }else{ $comimage = ""; } echo "&lt;div class = 'post' id = '$id'&gt;"; echo "&lt;a href = './visitpage.php?userpage='$user'&gt;$user&lt;br /&gt;&lt;img src='$img'/&gt;&lt;/a&gt;"; if(!empty($comimage)){ echo "&lt;img src='$comimage' class='comimage'&gt;&lt;/img&gt; &lt;h3&gt;$comments&lt;/h3&gt; "; }else{ echo "&lt;p&gt;$comments&lt;/p&gt;"; } echo "&lt;div style='border: 2px solid #000; height: 40px; margin-right: 20px; margin-left:90px; background: #F1F2F6'class='commentpanel' id='commentpanel_$id'&gt;"; if(in_array($id, $likedcomments)){ echo "&lt;img src='design/liked.png' class='like' /&gt;&lt;h4 class='liketext' id='liketext_$id'&gt;You and $likes&lt;/h4&gt;"; }else{ echo " &lt;a href='' id='like_$id'&gt;&lt;img src='design/like.png' class='like' /&gt;&lt;/a&gt;&lt;h4 class='liketext' id='liketext_$id'&gt;$likes&lt;/h4&gt;"; } if(in_array($id, $dislikedcomments)){ echo "&lt;img src='design/disliked.png' class='like' /&gt;&lt;h4 class='liketext' id='liketext_$id'&gt;You + $dislikes&lt;/h4&gt;"; }else{ echo " &lt;a href='' id='dislike_$id'&gt;&lt;img src='design/dislike.png' class='like' /&gt;&lt;/a&gt;&lt;h4 class='liketext' id='liketext_$id'&gt;$dislikes&lt;/h4&gt;"; } $link = urlencode($sname); echo " &lt;a href= '' id ='toggle_$id'&gt;&lt;img src='design/reply.png' class='like'/&gt;&lt;h4 class='liketext'&gt;Send Message&lt;/h4&gt;&lt;/a&gt; &lt;a href= './quote.php?text=$comments&amp;school=$link' &gt;&lt;img src='design/quote.png' class='like'/&gt;&lt;h4 class='liketext'&gt;Quote Post&lt;/h4&gt;&lt;/a&gt; &lt;/div&gt; &lt;div class = 'replypost' id='replypost_$id'&gt; &lt;form action='' method='POST'&gt; &lt;input type= 'hidden' name='user' id='user' value='$user' /&gt; &lt;input type= 'hidden' name='comment' id='comment' value='$comments' /&gt; &lt;input type='textarea' name='reply' id= 'reply' class='replycomment' /&gt; &lt;input type= 'submit' id='submit' name='submit' value=' Reply ' class='replybutton' /&gt; &lt;/form&gt; &lt;/div&gt; &lt;br/&gt;&lt;br/&gt;&lt;br/&gt; &lt;/div&gt;"; $counter++; } mysqli_stmt_close($stmt); } mysqli_close($mysqli); } </code></pre> <p>?></p> <pre><code> &lt;script&gt;&lt;!---- Load More Comments !----&gt; $("#loadmorecomments").click(function(){ $('div#loadmorecomments').show(); $.ajax({ url: "loadmorecomments.php?lastComment=" + $(".post:last").attr("id") + "&amp;sname=&lt;?php echo $link ?&gt;", success: function(html){ if(html){ $("#postspace").append(html); $("div#loadmorecomments").hide(); }else{ $("div#loadmorecomments").replaceWith("div#done"); } } }); }); $("a[id ^= 'toggle']").click(function(event){ event.preventDefault(); $("div [id='replypost_"+$(this).attr('id').replace('toggle_','')+"']").toggle(); }); var likecounter = 0; var dislikecounter = 0; $("a[id ^= 'like_']").click(function(event){ event.preventDefault(); var value = $(this).attr("id").replace('like_',''); $.ajax({ url: "./like.php?id="+value+"&amp;sname=&lt;?php echo $link ?&gt;", success: function(html){ if(html &amp;&amp; likecounter == 0 &amp;&amp; dislikecounter == 0 ){ $("#commentpanel_"+value).append("&lt;br/&gt;&lt;br/&gt;&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;You Like This Post&lt;/h4&gt;"); likecounter = 1; }else{ $("div#loadmorecomments").replaceWith("div#done"); } } }); }); $("a[id ^= 'dislike_']").click(function(event){ event.preventDefault(); var value = $(this).attr("id").replace('dislike_',''); $.ajax({ url: "./dislike.php?id="+value+"&amp;sname=&lt;?php echo $link ?&gt;", success: function(html){ if(html &amp;&amp; likecounter == 0 &amp;&amp; dislikecounter == 0 ){ $("#commentpanel_"+value).append("&lt;br/&gt;&lt;br/&gt;&lt;br /&gt;&lt;h4&gt;You Hate This Post&lt;/h4&gt;"); dislikecounter = 1; }else{ $("div#loadmorecomments").replaceWith("div#done"); } } }); }); &lt;/script&gt; </code></pre> <p>I was wondering why when I click on the anchor tags that are suppose to call those javascript functions, it just refreshes the page and no javascript functions are called.</p> <p>Note: The original page has the exact same format of posting comments and calling the javascript functions and it works perfectly. The only time it doesn't work is when the comments are loaded from the loadmorecomments.php file that is called.</p> <p>I know this is a tricky question if I can clarify any further please just let me know thanks. </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