Ajax代码在不应该运行时运行错误

时间:2015-05-20 02:25:02

标签: php ajax

我正在尝试做一个评论系统,但我的代码似乎在运行错误时,它不应该,任何人都知道为什么?这是代码:

view.php(我使用ajax从另一个文件中调用它,该文件虽然很好,但它也有jquery / ajax / etc库):

<?php
    include 'inc/dbc.php';
    include 'inc/functions.php';

    if(isset($_GET['user']) && !empty($_GET['user'])) {
        $username = $_GET['user'];
    } else {
        $username = $_SESSION['user'];
    }

    $my_name = $_SESSION['username'];

    if (loggedIn() == true) {
        $chech_posts = mysqli_query($mysqli, "SELECT * FROM wallposts WHERE posted_to = '$username' AND deleted = 0 ORDER BY posted_date DESC, posted_time DESC ");
        while ($row = mysqli_fetch_array($chech_posts, MYSQLI_BOTH)) {
                $post_id = $row['post_id'];
                $postby = $row['posted_by'];
                $postto = $row['posted_to'];
                $post = $row['post'];
                $post_date = $row['posted_date'];
                $post_time = $row['posted_time'];

                $p_avatar = getuser($postby, 'avatar');
                $p_first = getuser($postby, 'firstname');
                $p_last = getuser($postby, 'lastname');

?>
        <div class='wi wall' id='<?php echo $post_id;?>'>
            <div class='post'>
                <div class='post-container'>
                    <div class='post-header'>
                        <div class='pull-left'>
                            <?php
                                if ($postby == $postto) {
                                    echo '<b>'.$postby.'</b>';
                                } else {
                                    echo '<b>' . $postby . '</b> <i class="fa fa-chevron-right"></i> <b>' . $postto . '</b>';
                                }
                            ?>
                        </div>
                        <div class='post-img'><img src="images/users/<?php echo $p_avatar;?>" alt="<?php echo $p_first . ' ' . $p_last  . '\'s Profile Picture' ;?>" class="img-circle" align="middle"></div>
                        <div class='pull-right'><?php echo $post_date;?></div>
                        <div class='drop'>
                            <div class="dropdown">
                              <button class="btn btn-default dropdown-toggle car" type="button" id="postMenu" data-toggle="dropdown">
                                <span class="caret"></span>
                              </button>
                              <ul class="dropdown-menu" role="menu" aria-labelledby="postMenu">
                                <?php
                                    if ($username == $my_name) {
                                ?>

                                    <script type="text/javascript">
                                    $(function() {
                                        $('#delete').click(function(e){
                                            e.preventDefault();
                                            var post_id = "<?php echo $post_id;?>";
                                            $.ajax({
                                                type: 'POST',
                                                url: 'post_action.php?action=delete&post_id='+post_id,
                                                data: 'delete',
                                                success: function() {
                                                    alert('Successfull deleted post!');
                                                    $('#wallpos').load('view.php?user='+user);
                                                },
                                                error: function() {
                                                    alert('Post was not deleted!');
                                                }
                                            });
                                        });
                                    });
                                    </script>
                                    <li role="presentation"><a role="menuitem" tabindex="-1" href="#" id='delete'><i class='fa fa-trash-o fa-lg'></i> Delete</a></li>
                                    <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Another action</a></li>
                                <?php
                                    }
                                ?>
                                <li role="presentation" class="divider"></li>
                                <li role="presentation" class="dropdown-header">Report As</li>
                                <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Spam</a></li>
                                <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Inappropriate</a></li>
                              </ul>
                            </div>
                        </div>
                    </div>
                    <div class='post-body'>
                        <p><?php echo $post;?></p>
                    </div>
                    <div class='post-footer'>
                        <div class='lk-cmt-shr'>
                            <div><a href='' onClick="return false;">Like</a></div>
                            <div><a href='' id='opencomment'>Comment</a></div>
                        </div>
                        <span id='comments'>
                            <span id='com'></span>
                            <script>
                                $(function() {
                                    var post_id = "<?php echo $post_id;?>";
                                    $(document).ready(function() {
                                        $.get("loadcomments.php?post_id="+post_id);
                                    });
                                    var refresh = setInterval(function() {
                                    $.get("loadcomments.php?post_id="+post_id);
                                    }, 300000);
                                });
                            </script>
                            <span id='viewcomment'></span>
                            <div class='cform'>
                                <script type="text/javascript">
                                    $(function() {
                                        $(document).ready(function(){
                                                var post_id = "<?php echo $post_id;?>";
                                                    var commentElement = 'cinput-'+post_id;
                                                    var comment = $('#'+commentElement).val();
                                            $('#cinput-<?php echo $post_id;?>').keypress(function(e) {
                                                if (e.which == 13) {
                                                    e.preventDefault();
                                                    if (comment == '') {
                                                        alert('comment cannot be blank');
                                                    } else {
                                                        $.ajax({
                                                            type: 'POST',
                                                            url: 'sendcomment.php?post_id='+post_id,
                                                            data: $('#cf-'+post_id).serialize(),
                                                            beforeSend: function() {
                                                                $('#com').html('<i class="fa fa-spinner fa-spin"></li>');
                                                            },
                                                            success: function(data) {
                                                                $('#com').hide();
                                                                $("#viewcomment").load("loadcomments.php?post_id="+post_id);
                                                            },
                                                            error: function(data) {
                                                                alert('could not post your comment:' + data);
                                                            }
                                                        });
                                                    }
                                                }
                                            });
                                        });
                                    });
                                </script>
                                <form method='post' id='cf-<?php echo $post_id;?>'>
                                    <input type='text' class='form-control' id='cinput-<?php echo $post_id;?>' name='comment-<?php echo $post_id;?>' placeholder='Write a comment...'>
                                    <!-- <input type='submit' name='post-comment' id='pc' value=''> -->
                                </form>
                            </div>
                        </span>
                    </div>
                </div>
            </div>
        </div>
<?php

            }

    } else {
        echo '<li>You must be <a href="index.php">logged</a> in to view ' . $firstname . '\'s posts.</li>';
    }
?>

这是sendcomment.php:

<?php
    include 'inc/dbc.php';
    include 'inc/functions.php';

    header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");

    if(isset($_GET['user']) && !empty($_GET['user'])) {
        $username = $_GET['user'];
    } else {
        $username = $_SESSION['username'];
    }

    $my_name = $_SESSION['username'];

    $post_id = $_GET['post_id'];

    if (isset($_POST['comment-'.$post_id])) {
        $comment = stripcslashes(mysqli_real_escape_string($mysqli, $_POST['comment-<?php echo $post_id;?>']));
        $c_date = date('D j F, Y g:i a');
        $c_time = date('g:i:s');

        $post_comment = mysqli_query($mysqli, "INSERT INTO wallcomments(wallpost_id, commenter, comment, comment_date, comment_time) VALUES ('$post_id', '$my_name', '$comment', '$c_date', '$c_time')");
        if (!$post_comment) {
?>
    <script type="text/javascript">
        alert('Your comment was not posted');
    </script>
<?php
        } else {
?>
    <script type="text/javascript">
        alert('Your comment has been posted');
    </script>
<?php
        }
    }

?>

1 个答案:

答案 0 :(得分:0)

我认为以下问题导致了您的问题。

 var comment = $('#cinput-<?php echo $post_id;?>').val();

相反,您只能在之前格式化元素ID并使用它。如下所示,

var post_id = "<?php echo $post_id;?>";
var commentElement = 'cinput-'+post_id;
var comment = $("#"+commentElement).val();
alert(comment);

它会提醒您输入的文字。同样你也改变了这一行

 $('#cinput-<?php echo $post_id;?>').keypress(function(e) {

相关问题