Ajax不断刷新整个页面而不仅仅是评论部分

时间:2017-07-31 13:38:16

标签: php jquery ajax codeigniter

我正在使用codeigniter处理项目,我决定在注释部分实现ajax,以便在用户提交注释时不刷新整个页面。评论会自动显示,但页面会不断刷新。我不知道我在这里做错了什么,但任何帮助都会受到赞赏。

MODEL:

public function create_comments($post_id){
    $data = array(
        'post_id' => $post_id,
        'body' => $this->input->post('body'),
        'users_id' => $this->session->userdata('user_id')
    );
    return $this->db->insert('comments',$data);
} 

控制器:

public function create($post_id){

    //checking if user is logged in
    if(!$this->session->userdata('isLoggedIn')){
        redirect('users/login');
    }
    $slug = $this->input->post('slug');
    $data['post'] = $this->post_model->get_posts($slug);        
    $this->form_validation-> set_rules('body', 'Comment', 'required');

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

        $this->load->view('templates/header');
        $this->load->view('posts/view', $data);
        $this->load->view('templates/footer');

        $this->session->set_flashdata('comment_error', 'comment field is empty comment');
        redirect('posts/'.$slug,'refresh');

    } else {
        $this->comments_model->create_comments($post_id);
            redirect('posts/'.$slug);
        }
    }

查看:

<?php echo form_open_multipart('comments/create/'.$post['id'], 'id="comnt"'); ?>
    <div class="panel-footer">
    <!--//THIS IS THE FIELD WHICH COMMENTS WOULD BE ADDED-->
        <div class="">
            <input type="hidden" name="slug" value="<?php echo $post['slug']; ?>">
            <small class="text-center text-danger"><?php echo form_error('body'); ?></small>
            <div class="input-field">   
                <textarea id="icon_prefix2" name="body" class="materialize-textarea" rows="3"></textarea>
                <label for="icon_prefix2">Comments</label>
            </div>
            <button class="btn waves-effect waves-light" type="submit" name="action" id="submit">comment <i class="material-icons right">send</i></button>
        </div>
    </div>
</form>

AJAX:

$('#submit').click(function() {
    var form_data = {
        body: $("#icon_prefix2").val()
    };
    $.ajax({
        url: "<?php echo base_url();?>comments/create",
        type: "POST",
        data: form_data,
        success: function(msg) {
            alert("msg");
        }
    });
    return true;
});

3 个答案:

答案 0 :(得分:0)

使用jquery preventDefault()函数阻止提交表单

$('#submit').click(function(e) {
   e.preventDefault();
   var form_data = {
       body: $("#icon_prefix2").val()
   };
   $.ajax({
       url: "<?php echo base_url();?>"+"comments/create",
       type: "POST",
       data: form_data,
       success: function(msg) {
           alert("msg");
       }
   });
   return true;
});

答案 1 :(得分:0)

试试这个希望它起作用

 $('#submit').click(function(event) {
    e.preventDefault(); //stop actual action of browser
        var form_data = {
            body: $("#icon_prefix2").val()
        };
        $.ajax({
            url: "<?php echo base_url();?>comments/create",
            type: "POST",
            data: form_data,
            success: function(msg) {
                alert("msg");
            }
        });
        return true;
    });

更改按钮类型button而不是submit

    <button class="btn waves-effect waves-light" type="button" name="action" id="submit">comment
<i class="material-icons right">send</i>
 </button>

答案 2 :(得分:0)

尝试使用done属性设置成功处理程序。此外,由于这是DOM元素上的事件,因此您应该在DOM准备就绪时附加它:

$(document).ready(function() {
    $('#submit').click(function(event) {
        var form_data = {
            body: $("#icon_prefix2").val()
        };
        $.ajax({
            url: "<?php echo base_url();?>comments/create",
            type: "POST",
            data: form_data,
        }).done(function(msg) {
            alert(msg);
        }).fail(jqXHR, textStatus) {
            alert('ERROR: ' + textStatus);
        });

        // return is not necessary here
        // return true;
    });

    $("#comnt").submit(function(e) { // Prevent the form submitting
        e.preventDefault();
    });
});

由于这已经附加了一个事件,您应该从按钮中删除onclick="comment()"属性:

<button class="btn waves-effect waves-light" type="submit" id="submit">comment
                                <i class="material-icons right">send</i>
                              </button>