1

Initially, I'm looping out 10 comments from the database. Then, with the SHOWMORE button I'm looping out 4 more comments each time clicked. However, as from the 14th comment, the looped button, CONSENT THIS doesnt work(sort of a vote button)

ideas.php
<?php
$postshow = "SELECT * FROM ideas_1 ORDER BY consents DESC LIMIT 10";
$done = mysqli_query($conn, $postshow);
?>
<div class = 'comments'>
    <?php
while ($show = mysqli_fetch_assoc($done)) {
    $postid = $show['postid'];
    $userid = $show['userid'];
    $consents = $show['consents'];?>

    <?php echo$show['post'];?><br>
   <span class = 'counter'><?php echo$show['consents'];?></span> <span>Consents</span>

     <?php $asd = "SELECT * FROM consents WHERE dept = 'ideas' and task = '1' and postid = '$postid' and voterid = '$id'";
      $doit = mysqli_query($conn, $asd);
      $exist = mysqli_num_rows($doit);
      if ($exist == 0){ ?>
    <button class = 'consents' data-postid = '<?php echo$postid;?>' data-posterid ='<?php echo$userid;?>'
    data-voterid = '<?php echo$id;?>' data-consents = '<?php echo$consents;?>' >Consent This</button>
     <?php } ?>




<?php }
?>
</div>
<?php
     $more = "SELECT * FROM ideas_1";
     $more1 = mysqli_query($conn, $more);
     if (mysqli_num_rows($more1)>10) {
     ?>
     <button id = 'showmore'>Show more</button>
     <?php }?>
<script>


    var voterid = "<?php echo $id;?>"
    var commentCount = 10;

$(document).ready(function () {
   $('.consents').click(function(){
    var postid = $(this).data('postid');
    var consents = $(this).data('consents');
    var posterid = $(this).data('posterid');
    $(this).hide();
    $.post ("consentsideas.php", {
        dept: "ideas",
        task: "1",
        postid: postid,
        voterid: voterid,
        consents: consents,
        posterid: posterid
    });
    $(".counter").html(++consents);
   });

  $(".post_answer").click(function(){
    var answer = $(".answer").val();
    $.post ("answerideas.php", {
        answer: answer,
        id: voterid
            });
    $(".answer").val('');
    });
  $("#showmore").click(function(){
    commentCount = commentCount + 4;
    $('.comments').load("load-ideas.php", {
        commentNewCount: commentCount
        });
    });
}
  ); 
</script>
load-ideas.php
<?php
include ('header.php');
if (isset($_SESSION['id'])){
    $id = $_SESSION['id'];
} else {
    header("Location: index.php");
}
include('dbh.php');
$commentNewCount = $_POST['commentNewCount'];

$postshow = "SELECT * FROM ideas_1 ORDER BY consents DESC LIMIT $commentNewCount";
$done = mysqli_query($conn, $postshow);



while ($show = mysqli_fetch_assoc($done)) {
    $postid = $show['postid'];
    $userid = $show['userid'];
    $consents = $show['consents'];?>
   <div class = 'comments'>
    <?php echo$show['post'];?><br>
   <span class = 'counter'><?php echo$show['consents'];?></span> <span>Consents</span>

     <?php $asd = "SELECT * FROM consents WHERE dept = 'ideas' and task = '1' and postid = '$postid' and voterid = '$id'";
      $doit = mysqli_query($conn, $asd);
      $exist = mysqli_num_rows($doit);
      if ($exist == 0){ ?>
    <button class = 'consents' data-postid = '<?php echo$postid;?>' data-posterid ='<?php echo$userid;?>'
    data-voterid = '<?php echo$id;?>' data-consents = '<?php echo$consents;?>'>Consent This</button>
     <?php } ?>

   </div>


<?php }
?>


<script>
    $(document).ready(function(){ 
         $('.consents').click(function(){
    var postid = $(this).data('postid');
    var consents = $(this).data('consents');
    var posterid = $(this).data('posterid');
    $(this).hide();
    $.post ("consentsideas.php", {
        dept: "ideas",
        task: "1",
        postid: postid,
        voterid: voterid,
        consents: consents,
        posterid: posterid
    });
    $(".counter").html(++consents);
   });

});
</script>

What am I doing wrong?

  • You need to bind `click` event to the new buttons. – apires Jun 07 '17 at 17:50
  • Your code is vulnerable to [**SQL injection**](https://en.wikipedia.org/wiki/SQL_injection) attacks. You should use [**mysqli**](https://secure.php.net/manual/en/mysqli.prepare.php) or [**PDO**](https://secure.php.net/manual/en/pdo.prepared-statements.php) prepared statements with bound parameters as described in [**this post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – Alex Howansky Jun 07 '17 at 17:51
  • @doutriforce Isn't the click event already binded with the button? I'm confused – classifyed98 Jun 07 '17 at 17:53
  • 1
    @classifyed98, Roko already answered what i meant to say. – apires Jun 07 '17 at 17:57

1 Answers1

0

Since you populate your document dynamically with more .consents buttons - you need to dynamically delegate your click to those elements by using the jQuery .on() method with delegated events

$(staticParent).on(eventName, dynamicChild, callbackFn)

or in your case:

$(document /*or rather a static parent selector*/ ).on("click", ".consents", function) {

same goes for any other element that is dynamically generated.

Also don't use fetch_assoc, rather use prepared statements with PDO
Additionally don't trust input values without sanitization filtering using filter_input, filter_var etc.

Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313