简体   繁体   English

评论仅适用于第一篇文章

[英]Comments working for the first post only

I coded a status-box. 我编写了一个状态框。 It was successful. 它很成功。 Then I coded the comments system for the status. 然后我为状态编写了评论系统。 It all went fine. 一切都很好。 But there is a problem in it. 但它有一个问题。

Problem: When there is more than one status posted, it only allows comments on the first status. 问题:当发布多个状态时,它只允许对第一个状态发表评论。 If I try to post a comment on the second status, it won't load the AJAX request. 如果我尝试在第二个状态上发表评论,它将不会加载AJAX请求。 I tried checking the Dev console too, but it didn't show anything. 我也尝试检查开发控制台,但它没有显示任何内容。

Note: I load the statuses by AJAX into the main page. 注意:我将AJAX的状态加载到主页面中。 The comment is posted by a JavaScript function, which sends the $.ajax({type: "post"}); 评论由JavaScript函数发布,该函数发送$.ajax({type: "post"}); request to the file which retrieves the text, user ID and everything from the URL to save it into the database. 请求文件,该文件从URL检索文本,用户ID和所有内容以将其保存到数据库中。

Please tell me if there is any logic in it, and what could be the problem. 请告诉我它是否有任何逻辑,可能是什么问题。

jQuery AJAX function to POST comment jQuery AJAX函数用于POST注释

function send(id, post_id) {
    var pos = $("#txt").val();
    var post = pos.trim();
    if(post !== '') {
        $.ajax({
            url: 'comment.php?id='+id+'&post_id='+post_id+'&post='+post,
            type: "POST",
            success: function() {
                reload();
            }
        });
    }
}

This is how I call it: 这就是我所说的:

<textarea cols="70" id="txt" placeholder="Write a comment.." class="input-user"></textarea><br />
<br />
<input type="submit" value="Post" style="background: #f2f2f2; width: 60px; height: 40px; padding: 5px; border-radius: 5px;" onclick="javascript:send('<?php echo $_SESSION['id']; ?>', '<?php echo $post['id']; ?>');" />

The comment.php file: comment.php文件:

<?php 
include("includes/config.php");
include("includes/bbcode.php");
 $id = intval($_GET['id']);
$post_id = intval($_GET['post_id']);
 $pos = bbcode($_GET['post']);
$post = mysqli_real_escape_string($con, $pos);
  $post_comment = mysqli_query($con, "INSERT INTO comments(post_id, user_id, comment) VALUES('".$post_id."', '".$id."', '".$post."')") or die(mysqli_error($con));
 ?>

Again: All this is working only for the first post, ie, you can only comment on the first post and if you try to comment on the second or later posts AJAX doesn't even send a request.. 再说一遍:所有这些只适用于第一篇文章,即你只能对第一篇文章发表评论,如果你试着评论第二篇或后来的帖子,AJAX甚至都不发送请求。

The reason is because when you are using id="txt" repeatedly. 原因是因为你反复使用id =“txt”。 The script will only recognize the first one, after that you are toast. 脚本只会识别第一个,之后你就是吐司。 Also as @shut mentioned id's need to be unique. 另外,@ shut提到id需要是唯一的。 Change it to a class class="txt" 将其更改为class class="txt"

var pos = $(".txt").val();

Then call your function as such 然后调用你的函数

$('form').on('click','.txt',function() {
    send('id_1', 'post_id_1');
});

function send(id, post_id) {
    var pos = $(".txt").val();
    var post = pos.trim();
    if(post !== '') {
        $.ajax({
            url: 'comment.php?id='+id+'&post_id='+post_id+'&post='+post,
            type: "POST",
            success: function() {

            }
        });
    }
}

Assuming you have a separate form for each one 假设你有一个单独的表格

<form>
<textarea cols="70" class="txt" placeholder="Write a comment.." class="input-user"></textarea><br />
<br />
<input type="button" value="Post" class="txt" style="background: #f2f2f2; width: 60px; height: 40px; padding: 5px; border-radius: 5px;" onclick="javascript:send('<?php echo $_SESSION['id']; ?>', '<?php echo $post['id']; ?>');" />
</form>

id of any element must be unique. 任何元素的id必须是唯一的。 you are using comment box having id txt for all post then it always pick the data from the first textarea (for first post) which is blank in case of other post and AJAX doesn't send a request if the value of textarea is blank. 你正在使用所有帖子都有id txt的评论框然后它总是从第一个文本区域(第一个帖子)中选择数据,如果是其他帖子则为空白,如果textarea的值为空,则AJAX不发送请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM