javascript - 实时更新有问题

标签 javascript php ajax codeigniter

我正在 CodeIgniter 中进行实时更新,它几乎可以工作了。

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

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

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

这是 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>

编辑:

这是 Controller

   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');
  }
}
 }

最佳答案

问题在于您正在调用的操作。

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

您应该发布到仅显示您需要的数据的操作,而不显示其他内容

关于javascript - 实时更新有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24892118/

相关文章:

javascript - 根据 <select> #1 的选定选项更改 <select> #2 的选定选项

javascript - 使引导行在列内占据全高

javascript - Polymer:按下纸张按钮后获取纸张对话框内的纸张输入值 "ok"

PHP 从列中删除标签,然后更新数据库

javascript - jQuery 将数据发布到下一页

php - usleep() php5 使用了 40% 的空闲 CPU

php - 数组 PHP 中 undefined offset (在 for 迭代中)

javascript - 如何在sightly JAVA Script USE API中将对话框属性值传递给AJAX请求?

ajax - 带有Django(或任何网络框架)的AJAX的 "best practices"是什么

ajax - jsonp 数据到 tokenInput (或类似)插件