我正在创建一个人们可以对帖子进行投票的论坛。投票称为“同意”。
<?php
$postshow = "SELECT * FROM ideas_1 ORDER BY consents DESC LIMIT 10";
$done = mysqli_query($conn, $postshow);
//$show = mysqli_fetch_assoc($done);
while ($show = mysqli_fetch_assoc($done)) {
echo"<div class = 'comments'>";
echo"<br>";
echo"<br>";
echo $show['post'];
echo"<br>";
echo"<p class = 'counter'>";
echo $show['consents'];
echo"</p>";
echo"<br>";
$postid = $show['postid'];
$userid = $show['userid'];
$consents = $show['consents'];
$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){
echo"<button id = \"consents$postid\">Consent This</button>";
}
echo"</div>";
}
?>
<script>
var posterid = "<?php echo $userid;?>"
var postid = "<?php echo $postid;?>"
var voterid = "<?php echo $id;?>"
var consents = "<?php echo $consents;?>"
$(document).ready(function () {
$('#consents' + postid).click(function(){
$(this).hide();
$.post ("consentsideas.php", {
dept: "ideas",
task: "1",
postid: postid,
voterid: voterid,
consents: consents,
posterid: posterid
});
$(".counter").html(++consents);
});
$(".answer").mousedown(function(){
$(".post_answer").show();
});
$(document).click(function(event) {
if(!$(event.target).closest('.post_answer').length) {
if($('.post_answer').is(":visible")) {
$('.post_answer').hide();
}
}
});
$(".post_answer").click(function(){
var answer = $(".answer").val();
$.post ("answerideas.php", {
answer: answer,
id: voterid
});
$(".answer").val('');
});
}
);
</script>
但是,在单击时,while循环循环的任何按钮仅更新while循环的最后一次迭代的CONSENTS计数器。 正如你所看到的,我已经使按钮的ID唯一(我希望我做得正确)。所以有人能告诉我问题在哪里吗?
答案 0 :(得分:1)
问题不在你的PHP中;它在你的JavaScript中。您使用postid
循环的最后一次迭代中设置的最后一个$postid
设置while
一次。
解决此问题的一种方法是将click事件绑定到id(如#consents123
),而不是绑定到每个“Consent This”按钮上设置的类(如.consents
})。
然后,您需要在某处缓存所需的其他数据(如postid
),例如作为按钮上的数据属性或元素ID,然后在需要时将其检索到点击处理程序。
<强> PHP 强>
<button id = \"consents$postid\" class="consents" data-postid="$postid">Consent This</button>
<强> JS 强>
$('.consents').click(function() {
...
var postid = $(this).data('postid');
...
});
答案 1 :(得分:1)
@musicnothing 嘿,我编辑了一下我的代码。现在它有效。谢谢你!
while ($show = mysqli_fetch_assoc($done)) {
$postid = $show['postid'];
$userid = $show['userid'];
$consents = $show['consents'];?>
<div class = 'comments'>
<?php echo$show['post'];?><br>
<?php echo$show['consents'];?> <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>
var voterid = "<?php echo $id;?>"
$(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);
});
$(".answer").mousedown(function(){
$(".post_answer").show();
});
$(document).click(function(event) {
if(!$(event.target).closest('.post_answer').length) {
if($('.post_answer').is(":visible")) {
$('.post_answer').hide();
}
}
});
$(".post_answer").click(function(){
var answer = $(".answer").val();
$.post ("answerideas.php", {
answer: answer,
id: voterid
});
$(".answer").val('');
});
}
);
</script>
答案 2 :(得分:0)
echo"<button class = 'consents' data-postid = '$postid' data-posterid ='$userid' data-voterid = '$id' data-consents = '$consents' >Consent This</button>";
}
}
?>
<script>
$(document).ready(function () {
$('.consents').click(function(){
var posterid = $(this).data('posterid');
var postid = $(this).data('postid');
var voterid = $(this).data('voterid');
var consents = $(this).data('consents');
$(this).hide();
$.post ("consentsideas.php", {
dept: "ideas",
task: "1",
postid: postid,
voterid: voterid,
consents: consents,
posterid: posterid
});
$(".counter").html(++consents);
});
$(".answer").mousedown(function(){
$(".post_answer").show();
});
$(document).click(function(event) {
if(!$(event.target).closest('.post_answer').length) {
if($('.post_answer').is(":visible")) {
$('.post_answer').hide();
}
}
});
$(".post_answer").click(function(){
var answer = $(".answer").val();
$.post ("answerideas.php", {
answer: answer,
id: voterid
});
$(".answer").val('');
});
}
);
</script>
现在同意按钮不起作用。而且,我没有看到我的“发布”按钮($(".post_answer")
)