有LIVE UPDATING的问题

时间:2014-07-22 15:53:06

标签: javascript php ajax codeigniter

我在CodeIgniter中进行了 LIVE UPDATE ,它几乎正常工作。

只是一个小问题:当我点击按钮时,它也会在“响应”框中显示我的导航,这很奇怪。

当我刷新页面时,它被移除并且记录在那里。

这是一张解释我的意思的图片

这是JavaScript:

<script type="text/javascript">
$(document).ready(function() {

    //##### Add record when Add Record Button is click #########
    $("#FormSubmit").click(function (e) {
        e.preventDefault();
        if($("#contentText").val() ==='')
        {
            alert("Please enter some text!");
            return false;
        }

        var myData = 'content_txt='+ $("#contentText").val(); //build a post data structure
        jQuery.ajax({
            type: "POST", // Post / Get method
            url: "<?php echo site_url('admin/dashboard/index'); ?>", //Where form data is sent on submission
            dataType:"text", // Data type, HTML, json etc.
            data:myData, //Form variables
            success:function(response) {
                $("#responds").append(response);
            },
            error:function (xhr, ajaxOptions, thrownError) {
                alert(thrownError);
            }
        });
    });

});
</script>

编辑:

这是控制者        

   class Dashboard extends CI_Controller
   {
  public function __construct()
 {
   parent::__construct();

// Load libraries
$this->load->library('ion_auth');
$this->load->library('parser');
// Load models
$this->load->model('note_model');
// Load helpers
$this->load->helper('date');
// Set error delimiters
$this->form_validation->set_error_delimiters('<div class="alert alert-danger">', '</div>');
  }

    public function index()
    { 
     // Check if user is loged in
    if (!$this->ion_auth->logged_in())
    {
     redirect('auth/login');
     }
    else
    {
  // Create notes object
  $notes = new Note_model();
  // Order the notes by date post
  $notes->order_by('date_post', 'desc')->get();

  $recent_notes = array();
  foreach ($notes as $note)
  {
    $single_note = array
    (
      'id' => $note->id,
      'note_text' => $note->note_text,
      'date_post' => $note->date_post,
    );
    array_push($recent_notes, $single_note);
  }

  // Get the user id as an object
  $getinfo = $this->ion_auth->user($this->session->userdata('user_id'))->row();

  // Create a new note
  $createNote = new Note_model();
  $createNote->note_text = $this->input->post('content_txt');
  $created = date('Y-m-d H:i:s');
  $createNote->date_post = $created;

  // Validation rules
  $rules = $this->note_model->rules;
  $this->form_validation->set_rules($rules);

  $data = array
  (
    'admin_content' => 'admin/dashboard',
    'notes' => $recent_notes,
    'username' => $getinfo->{'first_name'} . ' ' . $getinfo->{'last_name'},
  );

  if ($this->form_validation->run() == FALSE)
  {
    $this->parser->parse('admin/template_admin', $data);
  }
  else
  {
    $createNote->save();
    redirect('admin/dashboard');
  }
}
 }

1 个答案:

答案 0 :(得分:1)

问题在于您正在呼叫的操作。

似乎admin/dashboard/index输出导航以及您想要显示的数据。

您应该发布一个只显示您需要的数据的动作,而不是其他任何内容