CodeIgniter& JQuery - 评论在首次发布后无效

时间:2013-11-16 10:30:07

标签: jquery codeigniter

感谢您阅读我的问题。

我一直试图找出代码上的问题。 在第一篇文章之后,我的评论无效。

当我点击“发布新消息”时,它将回显所有数据库信息。

抱歉,我仍然不允许上传任何图片。

JQuery的

$(document).ready(function() {
    $(function() {
        $('#postComment').click(function(e) {

            e.preventDefault();

            $.ajax({
                url: "<?= base_url() ?>index.php/comment/insertComments",
                type: 'POST',
                cache: false,
                data: $('#comment').serialize(),
                async: false,
                beforeSend: function() {
                    $('#ajax-loading').html('<img src="<?= base_url() ?>/assets/img/loading.gif" width="25px" height="25px" />');
                },
                success: function(data) {

                    $('#commentTextArea').val('');
                            var commentValue = $('#fieldValue').val();

                    if (data) {

                        $('#ajax-loading').html('<img src="<?= base_url() ?>/assets/img/loading.gif" width="25px" height="25px" />').hide();

                        var data = JSON.parse(data);

                            $('#commentHistory'+commentValue).hide().append(
                                    '<div style="background-color: lightblue; border-radius: 5px 5px 5px 5px;  margin-bottom: 5px; width: 100%;">' +
                                    '<a class="pull-left" href="#">' +
                                    '<img style="margin-top: 9px; margin-left: 5px; border-radius: 5px; padding-left: 5px;" class="media-object" id="sub-photo" src="../../images/' + data[0].Image + '" alt="'+ data[0].Image +'">' +
                                    '</a>' +
                                    '<div class="media-body">' +
                                    '<h6 style="color: gray; border-bottom: gray double thin; padding-bottom: 5px">' +
                                    '<i class="glyphicon glyphicon-time"></i>' +
                                    //TimeStamp
                                    '</h6>' +
                                    '<p class="media-heading" style="margin-top: -10px; font-size: 12px; font-weight: bolder; font-family: arial">' + data[0].FirstName + data[0].LastName + '</p>' +
                                    '<p style="width: 400px; word-wrap:break-word;">' +
                                    data[0].Description +
                                    '</p>' +
                                    '</div>' +
                                    '</div>').fadeIn(2000);

                    } else {
                        $('#searchInner').html('no results found').show();
                    }

                }
            });
        });
    });
});

控制器

function insertComments() {

    $this -> load -> helper(array('form'));
    $this -> load -> helper('date');
    $this -> load -> library('form_validation');

    $this -> form_validation -> set_rules('comment_message', 'Description', 'trim|required|min_length[1]');

    if ($this -> form_validation -> run() == FALSE) {

        $this -> load -> view('include/header');
        $this -> load -> view('register_failure');
        $this -> load -> view('include/footer');

    } else {

        $description = $_POST['comment_message'];

        //$description = htmlentities($this -> input -> post('comment_message', TRUE));
        $postID = $_POST['commentpost']['PostID']; //('commentpost');

        $session_data = $this -> session -> userdata('logged_in');
        $data['username'] = $session_data['username'];

        $studentID = $data['username'];

        $timestamp = time();

        $result = $this -> CommentModel -> insertComments($postID, $studentID, $description, $timestamp);

        $resultComment = $this -> CommentModel -> getComments();

        $this->output->set_header('Content-Type: application/json; charset=utf-8');
        echo json_encode($resultComment);
    }
}

1 个答案:

答案 0 :(得分:0)

好的,所以我在那里测试了一下。是的,这正是我认为它正在做的事情,有时它打破了JS代码,你的表单正常发送,没有AJAX。

所以我有几件事情:

  1. 你有很多JS错误,所以它会使其他JS代码停止,也许它会让你的表单正常运行而根本不使用AJAX
  2. 添加return false作为click回调的最后一条语句,因此会中断发送表单
  3. 验证空注释,因为它会生成500 internal error:)
  4. 当您刚创建评论时,您的日期不会显示
  5. 我测试了,我收到了您描述的错误,然后使用return false将代码更改为我的,并且确实解决了。

    另外,如果你想在控制器上insertComment函数做防弹,如果请求来自AJAX,则返回JSON,否则重定向......如下:

    if ($this->input->is_ajax_request()) {
        // respond JSON
    } else {
        // redirect
    }
    

    我希望这有助于你解决这个问题。 祝你好运。