2017-06-07 19 views
1

最初に、データベースから10個のコメントをループしています。次に、SHOWMOREボタンをクリックするたびに、クリックされるたびに4つのコメントが繰り返し表示されます。しかし、14日のコメント、ループされたボタン、同意THIS doesntの仕事(投票ボタンの一種)jQuery - データベースから追​​加のデータをループするとき、ループしたボタンが機能しません

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> 
負荷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> 

から、私は間違って何をやっています?

+0

あなたが新しいボタンに 'click'イベントをバインドする必要があります。 – doutriforce

+0

あなたのコードは[** SQLインジェクション**](https://en.wikipedia.org/wiki/SQL_injection)攻撃の脆弱性があります。 [** mysqli **](https://secure.php.net/manual/en/mysqli.prepare.php)または[** PDO **](https://secure.php.net/)を使用してください。 manual/en/pdo.prepared-statements.php)は、[** this post **](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql - インジェクション - イン - php)。 –

+0

@doutriforceクリックイベントが既にボタンにバインドされていませんか?私は混乱しています – classifyed98

答えて

0

あなたが動的により.consentsのボタンを使用して文書を取り込むので - あなたが動的にjQuery .on() method with delegated events

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

を使用するか、あなたのケースでそれらの要素にあなたのクリックを委任する必要があります。

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

は、動的に生成された他の要素と同じです。

も、むしろさらにfilter_inputを使用してサニタイズフィルタリングなしで入力値を信頼していないPDO
でプリペアドステートメントを使用し、fetch_assocを使用していない、filter_varなど

+0

この委任コードを見ているのは初めてです。これを使う方法や私にリンクを与える方法を教えてください。申し訳ありません、私は比較的初心者です! @Roko C. Buljan – classifyed98

+0

@ classifyed98私はリンクとそれを私の答えにどのように使うべきか両方を掲示しました。 –

+0

ありがとう! @ロコC. Buljan – classifyed98

関連する問題